PHP - 从url到变量的json_decode

时间:2017-12-04 19:41:45

标签: php arrays json

我有这个网址

try {
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    $ossClient->multiuploadFile($bucket, $object, $filePath);
} catch (OssException $e) {
    print $e->getMessage();
}

那是前2个的json输出(例如)

 https://poloniex.com/public?command=returnTicker

现在我希望获得所有数据到变量。喜欢... $ curreny = xxx; $ last = xxx,..

我已经尝试过了..

 {"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"},

适用于第一级。 然后我得到所有货币的清单。但是......我如何才能将所有其他数据输入货币?

第二个foreach?喜欢..

$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; }
我认为

没有意义!?

我想以那种形式出现

$ currency = BTC_BCN; $ last = 0.00003223; 等等..

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您不需要使用array_keys 您可以使用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

https://3v4l.org/UpkOm

<小时/> 如果要将数组中的项目用作变量,可以提取变量 我自己不使用这种方法,因为值无论如何都在数组中 我添加了这种方法,因为你的问题意味着它是你想要的 在每次迭代时,变量都将被覆盖,这就是为什么不推荐这种方法的原因 如果其中一个项目没有&#34; last&#34;所有其他变量都将被迭代中的新数据覆盖,除了&#34; last&#34;。
这意味着您同时在变量中同时拥有新旧数据 我的建议是使用数组而不是提取变量。

$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";
}

https://3v4l.org/aZv1s

答案 1 :(得分:0)

auth0.users.updateUserMetadata

您已经完成了获取信息的所有工作,只需要使用它:D

使用<?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>'; } ?> 查看数组中包含的内容及其具有的索引。