PHP-json_从url解码到变量
我有这个网址PHP-json_从url解码到变量,php,arrays,json,Php,Arrays,Json,我有这个网址 https://poloniex.com/public?command=returnTicker 这是前2个的json输出(例如) 现在我想把所有的数据都放到varialbles中。像$curreny=xxx$最后一个=xxx 我已经试过了 $url = "https://poloniex.com/public?command=returnTicker"; $response = file_get_contents($url); $obj = json_decode($resp
https://poloniex.com/public?command=returnTicker
这是前2个的json输出(例如)
现在我想把所有的数据都放到varialbles中。像$curreny=xxx$最后一个=xxx
我已经试过了
$url = "https://poloniex.com/public?command=returnTicker";
$response = file_get_contents($url);
$obj = json_decode($response,true);
$keys = array_keys((array)$obj);
foreach($keys as $result) {
$marketname = $result; }
这适用于第一级。
然后我得到所有货币的清单。但是如何将所有其他数据获取到货币
再来一次?像
foreach($result[$marketname] as $result2) { }
我觉得没道理
我想要那种形式的
$currency=BTC\U BCN;
$last=0.00003223;
等等
有什么想法吗?
<?php
$url = "https://poloniex.com/public?command=returnTicker";
$response = file_get_contents($url);
$obj = json_decode($response,true);
$keys = array_keys((array)$obj);
for($i=0;$i<count($keys);$i++)
{
echo 'Currency <strong>'.$keys[$i].'</strong>, last <strong>'.$obj[$keys[$i]]['last'].'</strong><br>';
}
?>
您已经完成了获取信息的所有工作,只需要使用它:D
使用
print\r($variable)
查看数组中的内容及其索引。不需要使用数组键。您可以使用foreach跟踪密钥
//$json = file_get_contents($url);
$json = '{"BTC_BCN":{"id":7,"last":"0.00000018","lowestAsk":"0.00000018","highestBid":"0.00000017","percentChange":"0.00000000","baseVolume":"58.73610647","quoteVolume":"328275043.97652394","isFrozen":"0","high24hr":"0.00000019","low24hr":"0.00000017"},"BTC_BELA":{"id":8,"last":"0.00001191","lowestAsk":"0.00001191","highestBid":"0.00001174","percentChange":"0.13536701","baseVolume":"6.33896473","quoteVolume":"572949.02508918","isFrozen":"0","high24hr":"0.00001200","low24hr":"0.00001033"}}';
$arr = json_decode($json, true);
Foreach($arr as $key => $val){
Echo "in ".$key . " last is ". $val['last'] ."\n";
}
输出为:
in BTC_BCN last is 0.00000018
in BTC_BELA last is 0.00001191
如果要将数组中的项用作变量,可以提取变量。
我自己不使用此方法,因为值无论如何都在数组中。
我添加此方法是因为您的问题暗示这是您想要的。
在每次迭代中,变量都将被覆盖,这就是为什么不建议使用此方法的原因。
如果其中一项没有“last”,则迭代中除“last”之外的所有其他变量将被新数据覆盖。
这意味着变量中同时包含新数据和旧数据。
我的建议是使用数组,而不是提取变量
$json = '{"BTC_BCN":{"id":7,"last":"0.00000018","lowestAsk":"0.00000018","highestBid":"0.00000017","percentChange":"0.00000000","baseVolume":"58.73610647","quoteVolume":"328275043.97652394","isFrozen":"0","high24hr":"0.00000019","low24hr":"0.00000017"},"BTC_BELA":{"id":8,"last":"0.00001191","lowestAsk":"0.00001191","highestBid":"0.00001174","percentChange":"0.13536701","baseVolume":"6.33896473","quoteVolume":"572949.02508918","isFrozen":"0","high24hr":"0.00001200","low24hr":"0.00001033"}}';
$arr = json_decode($json, true);
Foreach($arr as $key => $val){
Extract($val);
Echo $key ."\n";
Echo $last ."\n";
Echo $baseVolume ."\n\n";
}
显示一个JSON示例。
$json = '{"BTC_BCN":{"id":7,"last":"0.00000018","lowestAsk":"0.00000018","highestBid":"0.00000017","percentChange":"0.00000000","baseVolume":"58.73610647","quoteVolume":"328275043.97652394","isFrozen":"0","high24hr":"0.00000019","low24hr":"0.00000017"},"BTC_BELA":{"id":8,"last":"0.00001191","lowestAsk":"0.00001191","highestBid":"0.00001174","percentChange":"0.13536701","baseVolume":"6.33896473","quoteVolume":"572949.02508918","isFrozen":"0","high24hr":"0.00001200","low24hr":"0.00001033"}}';
$arr = json_decode($json, true);
Foreach($arr as $key => $val){
Extract($val);
Echo $key ."\n";
Echo $last ."\n";
Echo $baseVolume ."\n\n";
}