[英]PHP: get an element from complex JSON object
I'm trying to create a trading bot and at this point I should get the current market value for a cryptocurrency market. 我正在尝试创建一个交易机器人,这时我应该获得加密货币市场的当前市场价值。
At this point I would like to get the nested parameter "Last" in this JSON array: 此时,我想在此JSON数组中获取嵌套参数“ Last”:
array(3) { ["success"]=> bool(true)
["message"]=> string(0) ""
["result"]=> array(1) {
[0]=> array(13) { ["MarketName"]=> string(8) "USDT-BTC"
["High"]=> float(17399.99999999)
["Low"]=> float(16166)
["Volume"]=> float(7200.55684465)
["Last"]=> float(16486.5864853)
["BaseVolume"]=> float(119420929.2692)
["TimeStamp"]=> string(21) "2017-12-12T20:36:19.2" ["Bid"]=> float(16484)
["Ask"]=> float(16486.5864853)
["OpenBuyOrders"]=> int(12858)
["OpenSellOrders"]=> int(5529)
["PrevDay"]=> float(17369.85623056)
["Created"]=> string(23) "2015-12-11T06:31:40.633"
}
}
}
How can i get it and display it? 我如何获取并显示它?
This is the script where i get and decode the array 这是我获取并解码数组的脚本
private function send($method = null , $args = array() , $secure = true) {
if(empty($method)) return array("status" => false , "error" => "method was not defined!");
if($secure) $args["apikey"] = $this->apiKey;
$args["nonce"] = time();
$urlParams = array();
foreach($args as $key => $val) {
$urlParams[] = $key . "=" . $val;
}
$uri = $this->baseUrl . $method;
$argsString = join("&" , $urlParams);
if(!empty($urlParams)) {
$uri = $uri . "?" . $argsString;
}
$sign = $secure == true ? hash_hmac('sha512',$uri,$this->apiSecret) : null;
$uri = trim(preg_replace('/\s+/', '', $uri));
$ch = curl_init($uri);
if($secure) curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$execResult = curl_exec($ch);
if(curl_error($ch))
{
return array("status" => false , "error" => curl_error($ch));
}
$obj = json_decode($execResult, true);
return $obj["result"];
I succeeded in displaying the parameter ["result"]: how can i display something nested inside it? 我成功显示了参数[“ result”]:如何显示嵌套在其中的内容? Thank you in advance. 先感谢您。
Similar to the way you accessed the 'result' element of the initial array, you can access the 'last' element in the nested array like this: 与访问初始数组的'result'元素的方式类似,您可以像这样访问嵌套数组中的'last'元素:
$obj['result'][0]['Last'];
You're first looking at $obj['result'] which is an array and then accessing element 0, which is also an array. 您首先要查看$ obj ['result'],它是一个数组,然后访问元素0,它也是一个数组。 You're then accessing the key 'last' on this array. 然后,您将访问此阵列上的键“ last”。
You could test to see if $obj["result"] is an array then iterate on it, and continue drilling down until you find the thing you want. 您可以测试一下$ obj [“ result”]是否是一个数组,然后对其进行迭代,然后继续向下钻取,直到找到所需的对象为止。
if(is_array($obj["result"])) {
foreach($obj["result"] as $key => $thing) {
// do something with the $thing
// or do a 'defined()' check to see if the key(s)
// inside the object are there.
if(isset($thing["Last"])) {
// do something fancy with your new found info..
}
}
}
or just try to address it directly 或尝试直接解决
$obj["result"][0]["Last"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.