如何回显多个MAX值

时间:2015-01-11 10:29:18

标签: php arrays max

     echo max([1, 7, 7, 3]);

我知道这个例子的答案将是7.但由于有2个最高值,我希望答案是7& 7.有没有办法要求多个最高值?

2 个答案:

答案 0 :(得分:0)

$rresult = array();
$result[] = $fish
$max = count($fish);
if (count($tomatoes) > $max){
  $result = array();
  $result[] = $tomatoes;
}
elseif (count($tomatoes) == $max){
  $result[] = $tomatoes;
}
if (count($banana) > $max){
  $result = array();
  $result[] = $banana;
  $max = count($banana);
}
elseif (count($banana) == $max){
  $result[] = $banana;
}
if (count($chicken) > $max){
  $result = array();
  $result[] = $chicken;
  $max = count($chicken);
}
elseif (count($chicken) == $max){
  $result[] = $chicken;
}
return($result);

答案 1 :(得分:0)

您需要单独计算出最大值,然后查看是否有多个。

$array = [1, 7, 7, 3];
$max = max($array);

// This built-in function counts how many instances we have of each value
$counts = array_count_values($array);
if ($counts[$max] > 1)
{
    echo "There are " . $counts[$max] . " copies of the maximum value\n";
}

有关详细信息,请阅读on this function