数组值的PHP属性


PHP attribute of array value

如果之前有人问过这个问题,我很抱歉,但我不知道这个过程叫什么,所以我不知道如何搜索它…

我想打印出考试状元的名字。我已经构建了一个数组,返回最高分:

$tom = 90;
$dick = 80;
$harry = 70;
$n = array($tom, $dick, $harry);
arsort($n);
$keys = array_keys($n);
$top_score_1 = $n[$keys[0]];
echo $top_score_1; //prints 90

然而,我希望它打印名字'Tom'而不是分数。我如何将每个人的名字添加到他们的分数中,以便

echo $top_score_1; //prints Tom

非常感谢你的帮助,

p

我重新构造了数组-希望没有问题

<?php
$tom = 90;
$dick = 80;
$harry = 70;
$n = array('tom'=>$tom, 'dick'=>$dick, 'harry'=>$harry);
arsort($n);
$first_key = key($n); //key returns the current key, which will just be the first key in this case
echo $first_key; //tom