Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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", "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>
like image 330
Chi Chi Walker Avatar asked Jan 21 '23 05:01

Chi Chi Walker


1 Answers

There is also a one-liner for that:

$max = max( array_map("max", $multable) );
like image 191
mario Avatar answered Jan 28 '23 18:01

mario