I have an array within an array, for example:
[
[0, 20, 5],
[5, 0, 15],
[5, 10, 0]
]
I need to get the max number in each column.
- The max of
[0 , 5 , 5]is5, so that goes into the result array. - The max of
[20, 0, 10]is20, so that goes into the result array. - The max of
[5, 15, 0]is15, so that goes into the result array.
The final result array must contain [5, 20, 15].
First, the array has to be transposed (flip the rows and columns):
(taken from Is there better way to transpose a PHP 2D array? – read that question for an explanation of how it works)
Then, you can map the
maxfunction on the new array:Example: http://codepad.org/3gPExrhO
Output [I think this is what you meant instead of (5, 15, 20) because you said index 1 should be max of (20, 0, 10)]: