PHP数组用于查找max,min值

时间:2011-12-08 12:04:25

标签: php arrays

  

可能重复:
  min and max in multidimensional-array

如何在此数组中找到max和min值.Below是数组的类型。

    Array
    (
       [0] => Array
    (
        [uid] => 43543534
    )
    )

2 个答案:

答案 0 :(得分:0)

基于your previous question and its accepted answer,无论如何你都要遍历数组。当你这样做时,你可以计算出最大值:

$maxUid = 0;

foreach ($yourArray as $key => $val) {
    $maxUid = max( $maxUid, $val[ 'uid' ] );
    // do everything else
}

答案 1 :(得分:0)

$min = 99999999; //to make sure it's not below all the values
$max = 0;
foreach($array as $a){
    if($max < max($a)) $max = max($a);
    if($min > min($a)) $min = min($a); 
}

echo "min: $min, max: $max";