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?