I need to get a special entry from a json array and it should be matched by id.
{
"response": {
"count": 62,
"inventory": [
{
"id": 10,
"style": 982
},
{
"id": 20,
"style": 0
},
{
"id": 30,
"style": 1
} ]
}}
Everything I have right now is this code:
$matching_value = 10;
foreach($json_data as $key => $val){
if($val->id == $matching_value){
echo $val->id;
echo $val->style;
}
}
But it is not working and I don't know why. Does my approach not work with sub-entries?
PHP warning code not working on line 3;$json_datais the elements in"response", or the elements in"inventory"?return $key;ing before echo'ing, it will never echo... Is that what you were expecting?foreach ($json_data['response']['inventory'] as $key => $val) {idin JSON, but there also are elements withappidand withoutid. This gonna fail as never before.