如果值与php相同且最高,则返回数组键

如果值与php相同且最高,则返回数组键,php,arrays,Php,Arrays,我有这个阵列: $arr = array( 'English' => 70, 'Physics' => 65, 'Math' => 70, 'Chemistry' => 60, 'Geography' => 70, 'Biology' => 65 ); 请看用户在三个科目中得分最高。我想返回数组中所有最高数字的键。所以它应该返回到这里

我有这个阵列:

$arr = array(
         'English' => 70,
         'Physics' => 65,
         'Math' => 70, 
         'Chemistry' => 60,
         'Geography' => 70,
         'Biology' => 65

       );
请看用户在三个科目中得分最高。我想返回数组中所有最高数字的键。所以它应该返回到这里:
英语、Match和Geography
,因为它们的值相同且最高。我试过这个:

arsort($arr);
if(count(array_unique($arr)) === 1) {
     return array_keys($arr);

} 
但若数组中只有两个元素,而不是多个元素,它就可以工作。如果数组中有相同的最高值,如何实现返回键

注意:用户在物理和生物学方面也得到了同样的成绩。但这些数字并不是最高的。因此,它不应该返回非最大数字的键,即使它们的值相同。需要返回最高值和相同值的键

这是一种方式:

$by_score = array();
foreach ($arr as $key => $score) {
    if (!isset($by_score[$score])) {
        $by_score[$score] = array();
    }
    $by_score[$score][] = $key;
}
ksort($by_score);
$highest = end($by_score);

一种可能的解决方案是遍历数组并创建一个新数组,该数组使用点数作为键,并将具有该点数的类列表关联到每个键。按键(点)降序对新数组排序,获取第一个值:

// Create a new array that contains the classes indexed by points
$reverse = array();
array_walk(
    $arr,
    // Need to pass $reverse by reference to change it in the function
    function ($value, $key) use (& $reverse) {
        if (! isset($reverse[$value])) {
            $reverse[$value] = array();
        }
        // Put all the classes having the same number of points into the same list
        $reverse[$value][] = $key;
    }
);

// Sort by keys (the number of points) descending
krsort($reverse);
// Get the first entry from the sorted array
// It is the list of classes having the biggest number of points
$output = reset($reverse);

化学怎么样?这就是高度。对吗?抱歉我更新了数组。对。我在16分钟前没有注意到关于答案被编辑的通知,我对一个旧版本发表了评论:-)稍微更改了数组值我通过
print\r
array([65]=>array([0]=>Physics[1]=>Biology)[70]=>array([0]=>化学[1]=>地理[71]=>数组([0]=>英语[1]=>数学))
。我想做另一件事。我想用最后两个键71、70制作新阵列。因此,取最后两个键,像这样创建新数组<代码>$arr1=数组('English','Math')$arr2=数组(“化学”、“地理”)@Rudi谢谢你的解决方案。如何使用最后两个键创建两个新数组?出于某些原因,我需要它。不确定我是否理解正确,请尝试将最后一行更改为:
$highest=array\u slice($by\u score,-2)中的最后两项array@Rudi这就是我要的。非常感谢。我要去看看这个。谢谢