0

I have a json object from a curl get request.

// Curl Stuff
$resp = curl_exec($curl);
{"food": {"id":585897,"foodGroup":"meats","calories":1109,"foodTier":30}}

I saved it in a variable with json_decode

$data = json_decode($resp, TRUE);

I've tried accessing the data in a couple ways but I get no response

echo $data[0][1];
echo $data[0]['id'];

Also if someone can point me in the right direction of looping through this data I would appreciate that.

Mr.Smithyyy
  • 2,157
  • 12
  • 49
  • 95

1 Answers1

3

With the true parameter in your json_decode, you have associative array, so it must be:

echo $data['food']['id'];

Get the id by numeric:

$da = array();
foreach($data as $key=>$val){
  $da[$key] = array_values($val);
}
print_r($da);

echo $da['food'][0];
Mawia HL
  • 3,605
  • 1
  • 25
  • 46