here is my array sample $data[][]
;
Array( [0] => Array ( [id] => 1349
[rating1] => 1.9378838029981E-7
[rating2] => 1.1801796607774 )
[1] => Array ( [id] => 1350
[rating1] => 5.5499981876923E-7
[rating2] => 1.5121329727308 )
[2] => Array ( [id] => 1377
[rating1] => 0.00023952225410117
[rating2] => 2.1947077830236 )
[3] => Array ( [id] => 1378
[rating1] => 0.00022982302863634
[rating2] => 2.2135588326622 )
[4] => Array ( [id] => 1379
[rating1] => 0.00026272979843585
[rating2] => 2.2388295595073 )
[5] => Array ( [id] => 1380
[rating1] => 0.0002788640872546
[rating2] => 2.1815325502993 )
)
I want to find max($data[][rating?])
but return $data[id][max_rating?]
i.e. id
associated with the max
value.
Finding the max was easy for one particular, say rating1
, I used array_reduce as follows (this is inspired from this SO ):
$max = array_reduce($data, function($a, $b) {
return $a > $b['rating1'] ? $a : $b['rating1'];
});
Now I have two questions :
1. How can I extend above array_reduce to include rating2 ? I have other ratingX as well.
2. I do not want the max
value, rather the $data[][id]
associated with the max
.
I am not so much concerned about Q1, but the second one is important as I don't want to search through the array again to get associated $data[][id]
.
One line of thought is to use array_map instead of array_reduce, but I couldn't come up with a version which will pass on both [id]
and [rating?]
. Also, there are complications when I try to max()
multiple rating?
at one shot, as each rating will have different max
, which in turn associates with different [id]
.
EDIT : Just to be clear, I want all the respective id
s associated with respective max
of each rating?