获取多个最大变量名

Get multiple max variable name

我有6个变量,每个变量都是整数值,我想得到值最大的变量名。

变量是:$a = 2, $b = 3, $c = 3, $d = 4, $e = 4, $f = 4

如果我使用此代码:

$var = compact('a', 'b', 'c', 'd', 'e', 'f');
arsort($var);
$name = key($var);

变量 $name 将只包含 $d。问题是,如何得到$d, $e, $f?

尝试 array_keys() 搜索参数:

$var = compact('a', 'b', 'c', 'd', 'e', 'f');
arsort($var);
$max = reset($var);                      // get the maximum value (first item)
$results = array_keys($var, $max, true); // search for all the maximums and return the keys

或者不排序直接使用max():

$var = compact('a', 'b', 'c', 'd', 'e', 'f');
$max = max($var);                        // get the maximum value
$results = array_keys($var, $max, true); // search for all the maximums and return the keys