Reputation:
Sorry if this is rookie standard. The nesting really confuses me. Code here:
$json= '[{ "all":
"{"data":
[ {"name": "Kofi", "age":13}, {"name": "Jay", "age":17} ]
}"
}]' ;
$decode = json_decode($json);
$names= $decode->all->data->name;
// I want to retrieve "Kofi" and "Jay"
foreach ($names as $name){
echo $name;
}
I want to retrieve Kofi, Jay I get the error: Trying to get property 'all' of non-object
Upvotes: 0
Views: 96
Reputation: 550
I ran your json, it wasn't formatted properly. I have formatted it and this extra bit of code should do the job for you.
NOTE: The only difference was "all": "{"...."}"
changed to "all": { .... }
$json= '[
{
"all":
{
"data":
[
{ "name": "Kofi", "age":13},
{"name": "Jay", "age":17}
]
}
}
]';
$decode = json_decode($json);
foreach($decode[0]->all->data as $dec) {
echo $dec->name. '<br/>';
}
Upvotes: 1