steve8918
steve8918

Reputation: 1860

decoding an array from json into PHP and unable to access elements of array using keys

I have some JSON similar to the following:

{"internalArray": {"201": "A", "202": "B", "5": "C", "46": "D"}, 
 "data": "ABCDEFG", 
 "data2": "TSXPIIF"}

I use the following PHP code to decode it:

$jsonOutput = json_decode($output);

I would like to get access to the "internalArray" from the JSON data, so I reference it using the following:

$internalArray = $jsonOutput->{'internalArray'};

When I do a var_dump on $internalArray

object(stdClass)#4 (4) 
{ ["201"]=> string(1) "A" 
     ["202"]=> string(1) "B" 
     ["5"]=> string(1) "C" 
     ["46"]=> string(1) "D" 
} 

I figured out that I could cast this to an array, so I did the following:

$internalArray = (array) $jsonOutput->{'internalArray'};

However, now that I have this array, I can't appear to access it using values like

$internalArray["202"], $internalArray["201"], etc.

When I try to access it via the keys, it returns NULL. However, when I have code like this:

foreach ($internalArray as $key => $value)
{
  echo $key . "," . $value;
}

it prints out the values as expected, like "202,A", etc.

However, in the same code if I change it to

foreach ($internalArray as $key => $value)
{
  echo $key . "," . $internalArray[$key];
}

it doesn't work!

Can anyone explain why I can't access the values in $internalArray using the keys? Am I doing something fundamentally wrong here?

Upvotes: 3

Views: 2368

Answers (2)

NullUserException
NullUserException

Reputation: 85468

If you want an associative array, you can ask PHP for an associative array (see documentation for json_decode):

$jsonOutput = json_decode($output, true);

var_dump($jsonOutput['internalArray']);

Produces:

array(4) {
  [201]=>
  string(1) "A"
  [202]=>
  string(1) "B"
  [5]=>
  string(1) "C"
  [46]=>
  string(1) "D"
}

Back to your issue, your code would still work if the keys in the internal array were not numeric. What is happening here is a little peculiar: PHP doesn't allow you to have numeric strings (eg: '201', '46') as keys for an array.

Numeric strings keys will be converted to numbers keys instead. So when you do $arr['201'] PHP will look for $arr[201] instead. However, when you cast your object into an array, it looks like the array keys remain strings (eg: $arr['201']). Now the actual array has a numeric string key, but whenever you try to access it, PHP looks for an int key and never finds it, giving you NULL.

In fact, the documentation notes that:

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; (...)

Upvotes: 8

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

Because the data is not array it is an object. Hence you can't use it by this code snippet

foreach ($internalArray as $key => $value)
{
   echo $key . "," . $internalArray[$key];
}

and use for associate array json_decode($output, true);

Upvotes: 1

Related Questions