json multi array echo



带有:

<?php
$data = file_get_contents("http://query.yahooapis.com/v1/public/yql?env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json&q=select%20*%20from%20yahoo.finance.historicaldata%20where%20startDate=%272014-01-01%27%20and%20endDate=%272014-01-10%27%20and%20symbol=%27YHOO%27");
$myArray = json_decode($data, true);
/*
echo "<pre>";
var_dump( $myArray );
echo "</pre>";
*/
echo $myArray['query']['results']['quote'][0]['Close']," DayX";
?>

我能读出第一天的收盘数字。

->如何读取"关闭"的所有条目?->在这个例子中,它将是:

echo $myArray['query']['results']['quote'][0]['Close']," Day1";
echo $myArray['query']['results']['quote'][1]['Close']," Day2";
echo $myArray['query']['results']['quote'][2]['Close']," Day3";
...
echo $myArray['query']['results']['quote'][6]['Close']," Day7";

您需要使用foreach循环来执行此操作。

foreach ($myArray['query']['results']['quote'] as $k => $v) {
  echo $v['Close'] . " Day" . ($k+1) . "<br />";
}

在每行的末尾有<br />将添加一个换行符,假设这是浏览器的echo

最新更新