开发者

Find min/max values in a multidimensional array

开发者 https://www.devze.com 2023-02-17 13:41 出处:网络
I need to find the minimum and maximum in a multidimensional array in PHP, I have what I thought would work below but it keeps giving me a parse error, this is homework and I am not asking anyone to d

I need to find the minimum and maximum in a multidimensional array in PHP, I have what I thought would work below but it keeps giving me a parse error, this is homework and I am not asking anyone to do it for me but I am a beginner and any help would be appreciated.

<?php

/* 2 dimensional array in PHP - strictly an array of arrays */

$multable[] = array("11", "12", "15", "22", "41", "42");  
$multable[] = array("6", "7", "16", "17", "22", "23");  
$multable[] = array("1", "15", "16", "开发者_如何学Python;20", "22", "3");  

<table>
<?php
/* display a table from a 2D array */
for ($j=0;$j<3;$j++) {
    print "<tr>";
    for ($k=0;$k<6;$k++) {
        echo "<td>",$multable[$j][$k],"</td>";
    }
    print "</tr>";
    $max_value = 0;
    foreach ($multable as $myMax) {
        if ($max_value<$myMax) {
            $max_value = $myMax;
        }
    }
echo $max_value;
?>
</table>


There is also a one-liner for that:

$max = max( array_map("max", $multable) );


use max() and min() functions of php.


Max:

<?php
$multable = array();
$multable[] = array("11", "12", "15", "22", "41", "42");  
$multable[] = array("6", "7", "16", "17", "22", "23");  
$multable[] = array("1", "15", "16", "20", "22", "3");  
$max = -99999999;
foreach($multable as $sub){

  $tempMax = max($sub);

  if($tempMax > $max){
    $max = $tempMax;
  }

}

echo $max;

?>

You can figure out min :)


Your foreach iteration only does one dimension - each $myMax is one of your six element lists and not an individual scalar value. That's why your comparison doesn't work and the conditional is never true, you are trying to compare a scalar with an array. What you call $myMax would more appropriately be called $currentRow

This is ok because PHP has some functions to find the min and max of an array

http://us.php.net/manual/en/function.min.php
http://us.php.net/manual/en/function.max.php

$max_value = 0; $min_value = $multable[0][0];
foreach ($multable as $currentRow) 
{
    // COMPARE CURRENT ROW's MIN/MAX TO MIN/MAX_VALUE 
    // AND MAKE NEW ASSIGNMENT IF APPROPRIATE
}

Or hand this in and see what your teacher says:

function fComp ($f) {return function ($a,$b) use ($f) {return $f($a, $f($b));};}

$max = array_reduce($multable, fComp('max'), $multable[0][0]);
$min = array_reduce($multable, fComp('min'), $multable[0][0]);

echo "max: $max <br />";
echo "min: $min";

PS - in your earlier iterations to make the HTML table, it would be good form to lose the constants. Use count to get the length of the array instead - or better yet - use foreach like you do later on. (Even with foreach you would still need two of them nested, it doesn't iterate a 2-dimensional array element-by-element)


For Minimum value

echo min(array_map("min", $multable));

For Maximum Value

echo max(array_map("max", $multable));


$minArray = array();
        foreach($arrayVal as $arrI=> $arrK)
        {

            if($arrK == min($arrayVal ) )
            {
                array_push($minArray , $arrayVal );
            }
        }

print_r($minArray);

Here you go :)


I do not recommend calling min() or max() on each subarray, then calling the function again on the reduced array. This is making too many calls and won't be most efficient.

Instead, flatten the indexed array just once with a spread&merge technique, then call min() or max() just once on the flattened array.

Code: (Demo)

$flat = array_merge(...$multable);

printf(
    'Min: %d, Max: %d',
    min($flat),
    max($flat)
);

Output:

Min: 1, Max: 42

If you only need one or the other outcome, then don't bother with the temporary variable.

echo min(array_merge(...$multable));

Or

echo max(array_merge(...$multable));
0

精彩评论

暂无评论...
验证码 换一张
取 消