我将这个数组转换为从api收集的json_decode:
Array ( [Success] => 1 [Message] => [Data] => Array ( [0] => Array ( [TradePairId] => 1261 [Label] => $$$/BTC [AskPrice] => 3.1E-7 [BidPrice] => 3.0E-7 [Low] => 3.0E-7 [High] => 3.2E-7 [Volume] => 705593.52096319 [LastPrice] => 3.0E-7 [BuyVolume] => 25256894.050968 [SellVolume] => 18662564.012659 [Change] => 0 [Open] => 3.0E-7 [Close] => 3.0E-7 [BaseVolume] => 0.21205524 [BuyBaseVolume] => 1.29049546 [SellBaseVolume] => 25462.44174616 ) [1] => Array ( [TradePairId] => 1263 [Label] => $$$/DOGE [AskPrice] => 0.5899991 [BidPrice] => 0.46100022 [Low] => 0.46100044 [High] => 0.6 [Volume] => 16724.82996554 [LastPrice] => 0.49000028 [BuyVolume] => 44142637347.254 [SellVolume] => 431226.52315815 [Change] => -18.33 [Open] => 0.6 [Close] => 0.49000028 [BaseVolume] => 8561.99438073 [BuyBaseVolume] => 108392.69695843 [SellBaseVolume] => 8677417.2428937 )......
我只需要显示Label和LastPrice。
我正在尝试这个:
<?php
$jsondata = file_get_contents('https://www.cryptopia.co.nz/api/GetMarkets');
$data = json_decode($jsondata, true);
foreach ($data['0']['Label'] as $key=>$val) {
print_r($key);
}
//print_r($data);
?>
但未能成功。
答案 0 :(得分:2)
在下面找到您的解决方案:
$jsondata = file_get_contents('https://www.cryptopia.co.nz/api/GetMarkets');
$data = json_decode($jsondata, true);
foreach ($data['Data'] as $key=>$val) {
echo "Label=".$val['Label'];
echo "<br>";
echo "Last price=".$val['LastPrice'];
echo "<br>";
}
答案 1 :(得分:0)
您需要在Data
foreach
索引
foreach ($data['Data'] as $key=>$val) {
echo "label : ". $val['Label']." price :".$val['LastPrice']."<br>";
}
答案 2 :(得分:0)
您不需要执行foreach ($data['Data'] as $key=>$val)
,只需foreach ($data['Data'] as $val)
即可获得所需的结果,因为您未在任何地方使用$key
:
$jsondata = file_get_contents('https://www.cryptopia.co.nz/api/GetMarkets');
$data = json_decode($jsondata, true);
foreach ($data['Data'] as $val)
{
echo $val['Label'].' = '.$val['LastPrice'].'<br>';
}
另外,对静态字符串使用单引号,这样PHP就不必解析它们来查找变量或转义序列。