如何从JSON创建有效数组以在foreach循环中使用它

时间:2016-07-13 16:07:20

标签: php arrays json foreach

我有这个文字($ results):

{"data":{"summary":{"records":67,"page":1,"total":67,"hasMore":0},"reservations":[{"id":1111111,"property":"villadianna","from":"2016-07-18","to":"2016-07-25"},"pricing":{"price":1164.2400,"currency":"EUR","retail":1323},"clientInfo":{"firstName":"pera","lastName":"peric","email":"myemail@gmail.com"},"status":1,"offline":0},{"id":222222,"property":"villadianna", etc. ... ...

现在我需要获得reserations数组并进行foreach循环,所以我尝试:

...
$result = curl_exec($ch);
curl_close($ch);

$json = json_decode($result);
foreach ($json->reservations as $element) {
  print_r($element);
}

但我收到错误: enter image description here

如何使用foreach循环创建仅用于预订的html表?

1 个答案:

答案 0 :(得分:1)

...
$result = curl_exec($ch);
curl_close($ch);

$json = json_decode($result);
foreach ($json->data->reservations as $element) {
  print_r($element);
}