I have two arrays, where I can get from using $result[0] or $result[1].
I’ve done a print_r($results[0]) and a print_r($result[1]). they both give me the two different arrays which I want.
However, I’m not able to get any of my information if I do this
foreach($results[0] as $result){
echo $result['data']['id'];
}
I want to be able to differentiate the two arrays, so I want to be able to get information from the two.
I have done a json_decode on $results as well.
If someone can help me out here, that’ll be great! thanks
EDIT:
Array
(
[nonsense] => Array
(
[more] => information
[less] => less stuff
)
[data] => Array
(
[0] => Array
(
[id] => some id
)
[1] => Array
(
[eh] => some eh
)
)
)
Array
(
[more stuff] => Array
(
[more] => information
[less] => less stuff
)
[more data] => Array
(
[0] => Array
(
[freshstuff] => some id
)
)
)
In your example
$result['data']['id']doesn’t exist.idis inside yet another array.foreachis not useful in this situation because the data isn’t uniform. When you loop through$results[0]you get thenonsensekey on the first iteration and thedatakey on the second iteration. Furthermore, thenonsensekey is an array of key/value pairs, while thedatakey is an array of arrays of key/value pairs.$results[0]and$results[1]seem uniform, but they contain different keys.Update
foreach() doesn’t work with this kind of array. array_walk_recurisive() is generally a good choice in this situation.
displays
Depending on your needs, you may also find some of the other array functions useful.