简体   繁体   中英

maximum value in php array for each key value

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);
    }
}

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);
foreach ($res_arr as $k=>$subArray){
        $max = 0;
        foreach ($subArray as $id=>$value){
            if ($value>$max){
                $max = $value;
            }
        }
        $spanmaxArray[$k] = $max;
}

Try this.

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

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);

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM