The following will retrieve the largest y value:
<?php
$json_data = file_get_contents('a.json');
$json = json_decode($json_data, true);
$max_value = max($json[1]["data"]);
echo $max_value['y'];
?>
And the following will retrieve the array with the largest y value:
<?php
$json_data = file_get_contents('a.json');
$json = json_decode($json_data, true);
$max_value = max($json[1]["data"]);
print_r($max_value);
?>
The problem with your script was you were trying to access a non-existent array on line 3 and you weren't echoing out the $max_value array correctly.
Warning
The above code only works because OP's JSON when converted to an array has the element he wants to order his array by as the first indexed value.
So like @MatsLindh stated below in the comments, if you had a value with the key of 'x' instead of 'y', no matter where it's located in the JSON provided it's still the lowest key value placing it as the first indexed value in the array when converted from JSON to an array.
You can still have mix-matched values in the JSON provided as long as the element you want to order by has the lowest value key placing it as the first element indexed in the array. For example:
[{
"name": "Time",
"data": ["20:40", "20:45", "20:50"]
},
{
"name": "Values",
"data": [{
"y": 5774,
"v1": "Test1",
"v2": "20:40",
"Z" : 8000
},
{
"y": 5555,
"v1": "Test2",
"v2": "20:45",
"Abc": 6754
},
{
"y": 5235,
"v1": "Test3",
"v2": "20:50",
"v3": 6594
}]
}]
If the above JSON were passed through my first function it would return the desired result the OP was looking for, however.
[{
"name": "Time",
"data": ["20:40", "20:45", "20:50"]
},
{
"name": "Values",
"data": [{
"y": 5774,
"v1": "Test1",
"v2": "20:40",
"a": 897
},
{
"y": 5555,
"v1": "Test2",
"v2": "20:45"
},
{
"y": 5235,
"v1": "Test3",
"v2": "20:50"
}]
}]
With this set of JSON, the element with the key "a" messes the count order up since it's placed at the beginning of the array.
Conclusion
If you know that the first value of the array generated from the JSON will always be what you want to count by then my code is fine. Otherwise it's better for you to use @MatsLindh's solution.
$max_value['y']non$max['y']