2015-09-18 59 views
-3

與:JSON多陣列回波

<?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"; 
?> 

我可以讀出在第一天的關閉數。

- >我如何讀出'Close'的所有條目? - >在本例中是:

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"; 
+1

http://php.net/manual/en/control-structures.foreach.php ??? – AbraCadaver

回答

1

你需要使用一個foreach循環來做到這一點。

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

在每個線將添加換行符的末端具有<br />,假設這正在echo編到瀏覽器;

+0

作品完美,謝謝! :) –