如何在PHP中解析具有变量和显示最大值的JSON数组中的列? [重复]
This question already has an answer here:
I have a JSON array, I get it from the REST API using curl
and I want to get all the numbers from the "c" column and then find the maximum.
function part of my code
curl_setopt($ch, CURLOPT_URL, "https://api-domain.com/v3/instruments/" . $ticker . "/candles?&price=A&from=" . $first . "&to=" . $second . "&granularity=D");
// get stringified data/output. See CURLOPT_RETURNTRANSFER
$data = curl_exec($ch);
// get info about the request
$info = curl_getinfo($ch);
// close curl resource to free up system resources
curl_close($ch);
$json_string = $data;
$jsondata = ($json_string);
$obj = json_decode($jsondata,true);
print_r($jsondata); //below get the answer
//(print_r output)
{
"instrument":"EUR_USD",
"granularity":"D",
"candles":[
{
"complete":true,
"volume":32813,
"time":"2017-01-02T22:00:00.000000000Z",
"ask":{
"o":"1.04711",
"h":"1.04908",
"l":"1.03413",
"c":"1.04061"
}
},
{
"complete":true,
"volume":34501,
"time":"2017-01-03T22:00:00.000000000Z",
"ask":{
"o":"1.04076",
"h":"1.05009",
"l":"1.03907",
"c":"1.04908"
}
},
{
"complete":true,
"volume":52627,
"time":"2017-01-04T22:00:00.000000000Z",
"ask":{
"o":"1.04911",
"h":"1.06161",
"l":"1.04816",
"c":"1.06083"
}}]}
</div>
此问题已经存在 这里有一个答案: p>
-
从多维数组中的元素中获取最大值?
\ r
4 answers
span>
li>
ul>
div>
我有一个JSON数组, 我使用
curl code>从REST API中获取它,我想从“c” strong>列中获取所有数字,然后找到最大值 strong>。 p>
我的代码的功能部分 em> strong> p>
curl_setopt($ ch,CURLOPT_URL ,“https://api-domain.com/v3/instruments/”。$ ticker。“/ candle?&amp; price = A&amp; from =”。$ first。“&amp; to =”。$ second。“&amp; ;粒度= d“); \ n //获取字符串化数据/输出。 请参阅CURLOPT_RETURNTRANSFER $ data = curl_exec($ ch); //获取有关请求的信息 $ info = curl_getinfo($ ch); //关闭curl资源以释放系统资源 curl_close($ ch ); $ json_string = $ data; $ jsondata =($ json_string); $ obj = json_decode($ jsondata,true); print_r($ jsondata); //下面得到答案 code> pre>
//( print_r输出 em> strong>) p> \ ñ
{ “个仪器”: “EUR_USD” “个粒度”: “d”, “个蜡烛”:[\ N { “个完整”:真, “个体积 “:32813, ” 个时间 “:” 2017-01-02T22:00:00.000000000Z” “个问”:{ “○”: “1.04711”, “H”: “1.04908”, “L”: “1.03413”, “C”: “1.04061” \ N} \ N},\ N { “个完整”:真, “个体积”:34501, “时间” : “2017-01-03T22:00:00.000000000Z” “个问”:{ “○”: “1.04076”, “H”: “1.05009”, “L”: “1.03907” , “C”: “1.04908” \ N} \ N},\ N { “个完整”:真, “个体积”:52627, “个时间”:“2017-01-04T22:00 :00.000000000Z “ ”个问“:{ ”○“: ”1.04911“, ”H“: ”1.06161“, ”L“: ”1.04816“, ”C“:” 1.06083 “ }}]} code> pre> div>
You need to do following to get desired output.
- Decode json
- Map array
- Sort
Snippet
$data = '{
"instrument":"EUR_USD",
"granularity":"D",
"candles":[
{
"complete":true,
"volume":32813,
"time":"2017-01-02T22:00:00.000000000Z",
"ask":{
"o":"1.04711",
"h":"1.04908",
"l":"1.03413",
"c":"1.04061"
}
},
{
"complete":true,
"volume":34501,
"time":"2017-01-03T22:00:00.000000000Z",
"ask":{
"o":"1.04076",
"h":"1.05009",
"l":"1.03907",
"c":"1.04908"
}
},
{
"complete":true,
"volume":52627,
"time":"2017-01-04T22:00:00.000000000Z",
"ask":{
"o":"1.04911",
"h":"1.06161",
"l":"1.04816",
"c":"1.06083"
}}]}';
$arr = json_decode($data,true);
$c = array_map( function ($in){
return $in['ask']['c'];
}, $arr['candles']);
sort($c);
echo 'Min is: '.current($c)."
";
echo 'Max is: '.end($c);
Output
Min is: 1.04061
Max is: 1.06083
Live demo
Docs
You could build an array of the close prices using array_map()
with an anynomous function and then get the highest value using max()
:
// This will return an array of the close prices
$close_prices = array_map(function ($candle) {
return $candle['ask']['c'];
},
$jsondata['candles']); // <-- Of course send the candlesticks array
// Now get the highest value in the close prices array
$highest = max($close_prices);
echo 'Maximum EUR/USD value: 1 EUR = ' . $highest . ' USD';
// Maximum EUR/USD value: 1 EUR = 1.06083 USD
Same code, shorter:
$highest = max(array_map(function($c) { return $c['ask']['c']; }, $jsondata['candles']));