Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

php

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?

like image 379
steve8918 Avatar asked Dec 19 '25 00:12

steve8918


1 Answers

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; (...)

like image 63
NullUserException Avatar answered Dec 20 '25 18:12

NullUserException



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!