PHP - find min / max in a multidimensional array

I need to find the minimum and maximum in a multidimensional array in PHP, I have what I thought would work below, but it continues to give me a parsing error, this is homework, and I do not ask anyone to do this 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", "20", "22", "3");  


# ---------------------------------------------
?>
<html>
<head>
<title>An array of arrays in PHP</title>
</head>
<body bgcolor=white>
<h2>Two dimensional array</h2><br>
<table border=2 cellpadding=2 cellspacing=2>

<?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>
</body>
</html> 
+3
source share
6 answers

There is one line for this:

$max = max( array_map("max", $multable) );
+7
source

use max()and min()php function.

+1
source

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;

?>

min:)

+1

foreach - $myMax , . , , . , $myMax, $currentRow

, PHP min max

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 MIN/MAX TO MIN/MAX_VALUE 
    // AND MAKE NEW ASSIGNMENT IF APPROPRIATE
}

, :

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 - , HTML, . count, - - foreach, . ( foreach , )

0

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

echo max(array_map("max", $multable));
0
$minArray = array();
        foreach($arrayVal as $arrI=> $arrK)
        {

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

print_r($minArray);

:)

0

All Articles