2

I want to find out the maximum value of the each key in the array

$arr1= array(
    0=>array(1,2,3),
    1=>array(2,4,6),
    2=>array(25,4,5));
}

I want to find the maximum value for each value for the corresponding key The final output shuld be

$max_array = array(25,4,6);

with the below code i get error max(): When only one parameter is given, it must be an array in

foreach ($res_arr as $k=>$subArray) {
    foreach ($subArray as $id=>$value) {
        $spanmaxArray[$id] = max($value);
    }
}
4
  • that is corrected.comparing the [0],[1][2] the first element 1,2,25.The max valus is 25,same way for other elements also.the return value should be also an array. Commented Feb 4, 2014 at 14:05
  • Bob's your uncle. Read the manual ! Commented Feb 4, 2014 at 14:06
  • 1
    @HamZa I think he wants to output the max for each column Commented Feb 4, 2014 at 14:07
  • 1
    @MarioJohnathan Ah I see, array_column() might be handy (PHP 5.5+). demo Commented Feb 4, 2014 at 14:17

5 Answers 5

4

First, rotate array to makes columns as lines

$arr1= array(
    0=>array(1,2,3),
    1=>array(2,4,6),
    2=>array(25,4,5)
);

$arr2 = array();

foreach($arr1 as $subArray) {
    foreach($subArray as $k=>$v) {
        $arr2[$k][] = $v;
    }
}

Then, use array_map as @HamZa did

$max_array  = array_map('max', $arr2);
print_r($max_array);
Sign up to request clarification or add additional context in comments.

1 Comment

Spot on answer. Works well!
2
foreach ($res_arr as $k=>$subArray){
        $max = 0;
        foreach ($subArray as $id=>$value){
            if ($value>$max){
                $max = $value;
            }
        }
        $spanmaxArray[$k] = $max;
}

Try this.

Comments

2

Definitely the shortest version:

$max_array = array_map('max', $arr1);

array_map() takes each element array of $arr1 and applies max() to it.

Edited after seeing that max in each column position is desired:

$sorted = array();
for ($i = 0; $i <=2; $i++) { 
    $sorted[$i] = array_column($input, $i);
}

This loops over the array and applies array_column() to each (requires PHP 5.5).

So:

$sorted = array();
for ($i = 0; $i <=2; $i++) { 
    $sorted[$i] = array_column($arr1, $i);
}
$max_array = array_map('max', $sorted);

working version

1 Comment

OP wanted to get the max of each column so @Mario Johnathan's answer is better.
0

You only need one loop, because the max function takes an array as a parameter. But the answer you are giving is wrong, unless I've misunderstood the problem.

I think what you're after is:

$max_array = array_map('max', $arr1);

Comments

0

All of the earlier answers are working too hard.

  • Use array_map() as the iterating function.
  • Unpack your multidimensional array with the "spread/splat operator" (...); in other words, convert your single array containing three subarrays into three separate arrays.
  • Call max() on the three elements as they are synchronously iterated by array_map().

Code: (Demo)

$arr1 = [
    [1, 2, 3],
    [2, 4, 6],
    [25, 4, 5],
];

var_export(array_map('max', ...$arr1));

The above is a more versatile and concise version of the following one-liner which has the same effect on the sample array:

var_export(array_map('max', $arr1[0], $arr1[1], $arr1[2]));

Output:

array (
  0 => 25,
  1 => 4,
  2 => 6,
)

*A note for researchers: if processing a multidimensional array that has associative first level keys, you will need to index the first level so that the splat operator doesn't choke on the keys.

...array_values($array)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.