I want to find the time
and temp
in the row with maximum temp
value in a 2d array.
Here is sample data for my weather array:
[
['time' => '00:00', 'temp' => '15.1'],
['time' => '00:00', 'temp' => '15.1'],
['time' => '00:01', 'temp' => '15.1'],
['time' => '00:02', 'temp' => '15.1'],
['time' => '00:02', 'temp' => '15.1'],
['time' => '00:02', 'temp' => '15.0'],
['time' => '00:03', 'temp' => '15.0'],
]
I’ve tried a few google searches and it finds the max temp, but I cannot work out how to get the time associated with that.
This is also using WordPress, so I am trying to reduce the WP_Queries I have setup currently.
2
Answers
Declare a result array and while iterating, only update the result array if the result array is empty or if the new temp value is greater than the stored temp value.
Code: (Demo)
The result will be the first occurring row with the max temperature.