here is the nutrionix json ouput
<?php
$json = '{
"total": 3,
"max_score": 2.2296956,
"hits": [
{
"_index": "9fa412b9-1dbc-4e35-be7a-0c0c9c8c8e16",
"_type": "item",
"_id": "51c3d2b297c3e6d8d3b51ecf",
"_score": 2.2296956,
"fields": {
"nf_calories": 170,
"item_name": "Laksa Paste"
}
},
{
"_index": "9fa412b9-1dbc-4e35-be7a-0c0c9c8c8e16",
"_type": "item",
"_id": "51c35fb297c3e69de4b01eee",
"_score": 1.3439745,
"fields": {
"nf_calories": 90,
"item_name": "Laksa Coconut Curry, Family Size, Medium"
}
},
{
"_index": "9fa412b9-1dbc-4e35-be7a-0c0c9c8c8e16",
"_type": "item",
"_id": "51c3610597c3e69de4b0275d",
"_score": 1.1046534,
"fields": {
"nf_calories": 40,
"item_name": "Spice Paste For Noodles, Laksa, Coconut Curry Noodles, Mild"
}
}
]
}';
The i decode $json using json_decode and tried to get nf_calories
$data= json_decode($json);
foreach ($data -> hits as $hit){
foreach($hit-> fields as $field){
echo $field->nf_calories;
}
}
?>
the error:
Notice: Trying to get property of non-object in C:\wamp\www\simple_light\testing.php on line 51
when i tried to echo $field;
here is the value
170Laksa Paste90Laksa Coconut Curry, Family Size, Medium40Spice Paste For Noodles, Laksa, Coconut Curry Noodles, Mild
i dont know where i do wrong.