php从相同的值获取最大密钥

时间:2014-01-19 23:57:29

标签: php arrays key max

如何从相同的值获取最大密钥?

示例:

 1 => 32
 2 => 32
 3 => 32
 4 => 5
 5 => 5
 6 => 11
 7 => 11

结果:

 3 => 32
 5 => 5
 7 => 11

2 个答案:

答案 0 :(得分:2)

假设值都是整数

$array = [
  1 => 32,
  2 => 32,
  3 => 32,
  4 => 5,
  5 => 5,
  6 => 11,
  7 => 11,
];

$result = array_flip(array_flip($array));
var_dump($result);

答案 1 :(得分:0)

krsort($array);
ksort(array_unique($array);