简体   繁体   English

使用PHP在数组上的最大和最小值

[英]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? 有谁知道如何获取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) 最大(12.9)最小(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. 我想这不是最有效的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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