简体   繁体   中英

MAX & MIN values on array using PHP

Does anybody knows how can I get the max and min value of the 2nd and 3rd columns in PHP?

$ar = array(array(1,  10,   9.0,   'HELLO'),
            array(1,  11,  12.9,   'HELLO'),
            array(3,  12,  10.9,   'HELLO'));

Output should be like:

max(12.9) min(10)

Another option

<?php

function array_rotate( $array )
{
    $rotated = array();
    foreach ( $array as $rowIndex => $col )
    {
        foreach ( $col as $colIndex => $value )
        {
            $rotated[$colIndex][$rowIndex] = $value;
        }
    }
    return $rotated;
}

$ar = array(array(1,  10,   9.0,   'HELLO'),
            array(1,  11,  12.9,   'HELLO'),
            array(3,  12,  10.9,   'HELLO'));

$ar = array_rotate( $ar );

echo max( $ar[2] ), "\n", min( $ar[1] );
<?php
$ar = array(array(1,  10,   9.0,   'HELLO'),
            array(1,  11,  12.9,   'HELLO'),
            array(3,  12,  10.9,   'HELLO'));
function col($tbl,$col){
    $ret = array();
    foreach ($tbl as $row){
        $ret[count($ret)+1] = $row[$col];
    }
    return $ret;
}
print (max(col($ar,2))."\n");
print (min(col($ar,1))."\n");
?>

is this what you look for? I guess its not the most efficient way.

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