1

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"
}}]}
Patrick Mevzek
  • 10,995
  • 16
  • 38
  • 54
  • Seeing the output this is probably not the `print_r` result. To print the JSON in a nicer way: `echo '
    ' . print_r($jsondata, true) . '
    ';`
    – AymDev Aug 16 '18 at 14:11
  • Also, what have you tried ? Please show your attempt. – AymDev Aug 16 '18 at 14:12
  • `echo $obj['candles'][0]['ask']['c']` he me lifts the number of only the first array 1.04061 – Vasiliy Mishura Aug 16 '18 at 14:28
  • So write a foreach loop to process the array `foreach ( $obj['candles'] as $candles)` – RiggsFolly Aug 16 '18 at 14:34
  • @RiggsFolly [follow link Get the maximum](https://stackoverflow.com/questions/2189479/get-the-maximum-value-from-an-element-in-a-multidimensional-array) `$obj = json_decode($jsondata,true); $max = 0; foreach($obj as $objj) { if($objj->candles->ask->c > $max) { $max = $objj->c; } var_dump($max); }` **I tried, but why the not goes** output ` int(0) int(0) int(0)` – Vasiliy Mishura Aug 16 '18 at 15:07

2 Answers2

0

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']));
AymDev
  • 6,626
  • 4
  • 29
  • 52
0

You need to do following to get desired output.

  1. Decode json
  2. Map array
  3. 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)."\n";
echo 'Max is: '.end($c);

Output

Min is: 1.04061
Max is: 1.06083

Live demo

Docs

  1. array_map
  2. sort
Shahnawaz Kadari
  • 1,423
  • 1
  • 12
  • 20