1

I'm getting back a JSON string from a third party API. When I use json_decode($json, true) to put it into an array, I end up with a nested array. It seems to me it shouldn't be like this. Can someone explain if this is correct behavior?

$obj = getStatus('100000043');
var_dump($obj);

getStatus calls the third-party API. The result of var_dump is:

string(245) "[{"Status":"Processing","OrderNum":"87","InvoiceNum":"0","PoNum":"100000043","ShippingCost":"","TrackingNum":"","ShippingWeight":"","cust_num":"123","entryTime":"2012-02-02 15:19:05","branch":"0","CustPoNum":"43","brord":"12345"}]"

Ok, so I decode to an array:

$myArray = json_decode($obj, true); 
var_dump($myArray);

result is:

array(1) { [0]=> array(12) { ["Status"]=> string(10) "Processing" ["OrderNum"]=> string(5) "87686" ["InvoiceNum"]=> string(1) "0" ["PaPoNum"]=> string(9) "100000043" ["ShippingCost"]=> string(0) "" ["TrackingNum"]=> string(0) "" ["ShippingWeight"]=> string(0) "" ["cust_num"]=> string(5) "64366" ["entryTime"]=> string(19) "2012-02-02 15:19:05" ["branch"]=> string(1) "8" ["CustPoNum"]=> string(9) "100000043" ["brord"]=> string(6) "887686" } }

The array I want is nested within $myArray[0] ... How to get a value out:

$myArray[0]['Status']

From the docs and examples I would expect $myArray['Status'] to work.

Do i misunderstand or I'm doing something wrong or is there something funky about the API result?

2
  • Why don't you just workaround it with $myArray = $myArray[0]; just a suggestion... Commented Feb 3, 2012 at 22:14
  • What docs are you referring to? Commented Feb 3, 2012 at 22:14

2 Answers 2

3

with json encoded data, when you have code that starts with a square brace, then it contains data with curly braces: [{data}], it's always going to be a nested array.

It's simple enough to access the data using $myArray[0]['Status']. You can also assign $myArray[0] to $myArray like this:

$myArray = $myArray[0];

Then you can access all elements of the array using this syntax:

$myArray['Status'];
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! working with json is new for me. now i know that accessing it like this is not a hack :)
2

string(245) "[{"Status":"Processing","OrderNum":"87","InvoiceNum":"0","PoNum":"100000043","ShippingCost":"","TrackingNum":"","ShippingWeight":"","cust_num":"123","entryTime":"2012-02-02 15:19:05","branch":"0","CustPoNum":"43","brord":"12345"}]"

Look at the first and last characters of that string: [ and ]. This is JSON (and Javascript) syntax for an array. So the object that you actually want is the first (and only) member of a JSON array. So when you decode it with PHP, PHP reflects the JSON structure and puts the object you want as the first (and only) member of an array.

You have two choices. You could just get extract the data you want with array syntax after decoding it:

$myArray = $myArray[0];

Alternatively, you could remove the first and last characters with substr, so the array literal isn't present. Note that this is much less stable than the first version.

$myArray = json_decode(substr($obj, 1, -1), true);

Note that this will break if you have more than one object returned, so don't use it if that's a remote possibility.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.