I have an array with these values:
Array
(
[0] => Array
(
[0] => John
[1] => 2012-03-29
[2] => 1
)
[1] => Array
(
[0] => Doe
[1] => 2012-03-30
[2] => 1
)
[2] => Array
(
[0] => John
[1] => 2012-03-31
[2] => 2
)
[3] => Array
(
[0] => Doe
[1] => 2012-03-31
[2] => 5
)
[4] => Array
(
[0] => John
[1] => 2012-04-02
[2] => 5
)
[5] => Array
(
[0] => John
[1] => 2012-04-02
[2] => 21
)
)
I’m trying to get the maximum value of array[][2] for each date in array[][1].
For example, in the array
Array[2][1] = 2012-03-31 | Array[2][2] = 2
Array[3][1] = 2012-03-31 | Array[3][2] = 5
Out of those two same days I want to keep Array[3] and discard Array[2].
I tried a foreach function:
foreach ($days as $k) {
$max_vals[] = array($k[0], $k[1], max($k[2]));
}
But found that is not working with the days. How can I sort this array keeping the highest value for each day, and discard the rest?
The following code will iterate across each value in $data, either added newly encountered dates to the
$cacheor checking if it is larger.When finished,
$cachewill be an array with the values being dates and the keys the max value ofarray[][2].