当我执行以下操作时
$arr['exchange'] = array('to' => $to, 'rate' => $result[0]);
该代码有效,但只打印一次。
当我这样做时
$arr['exchange'] .= array('to' => $to, 'rate' => $result[0]);
它打印出来
{"from":"NZD","exchange":"ArrayArrayArrayArray"}
什么是正确的循环方式,以便它可以在交换数组中设置 6 个子数组?
这是我的完整代码
<?php
$currencies = array("USD", "NZD", "KWD", "GBP", "AUD");
foreach ($currencies as $from)
{
$arr = array();
$arr['from'] = $from;
//$arr['exchange'] = array();
foreach ($currencies as $to)
{
if($from != $to)
{
$url = 'http://finance.yahoo.com/d/quotes.csv?f=l1d1t1&s='.$from.$to.'=X';
$handle = fopen($url, 'r');
if ($handle) {
$result = fgetcsv($handle);
fclose($handle);
}
$results = $result[1].$result[2];
$arr['exchange'] = array('to' => $to, 'rate' => $result[0]);
}
}
print json_encode($arr);
print"<br><br>";
}
?>