0

I have the follwoing being returned by a json call:

Array
(
    [0] => Array
        (
            [so] => SO0040024
        )

    [1] => Array
        (
            [coid] => 4824
        )

    [2] => Array
        (
            [sdkstatus] => 7
        )

    [3] => Array
        (
            [sdkstatus] => pass
        )

    [4] => Array
        (
            [invoicenumber] => INV0063955
        )

    [5] => Array
        (
            [invoiceamount] => 9437.24
        )

    [6] => Array
        (
            [invoicestatus] => pass
        )

    [7] => Array
        (
            [invoicestatus] => fail
        )

)

How do I extract each value out of the array? for example I want invoicenumber, INV0063955.

Thanks, Ryan

2
  • Why is it multidimensional? Theres no need to. Try to flatten the output so you can loop through it more easily. Commented Aug 29, 2014 at 10:11
  • Why do some keys appear twice, like sdkstatus and invoicestatus? Commented Aug 29, 2014 at 10:13

1 Answer 1

1

That's a horrible way to structure your data. Instead of nesting each property in its own array, they should be keys of the main array. But if you're stuck with it:

foreach ($array as $element) {
    if (isset($element['invoicenumber'])) {
        $invoicenumber = $element['invoicenumber'];
        break;
    }
}

You could also turn it into a more sane associative array like this:

$newarray = array();
foreach ($array as $element) {
    foreach ($element as $key => $value) {
        $newarray[$key] = $value;
    }
}

However, this won't deal with the repeated keys, it will just save the last one. I'm not sure how this is supposed to be handled in your data. Maybe those elements should actually be arrays of values?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.