我有以下数组
Array
(
[0] => Array
(
[title] => Title of company1
[link] => https://companywebsite1.com
[result] => 29.814814815
)
[1] => Array
(
[title] => Title of company2
[link] => https://companywebsite2.com
[result] => 143.723259762
)
[2] => Array
(
[title] => Title of company3
[link] => https://companywebsite3.com
[result] => 212.202797203
)
[3] => Array
(
[title] => Title of company4
[link] => https://companywebsite4.com
[result] => 127.884615385
)
[4] => Array
(
[title] => Title of company5
[link] => https://companywebsite5.com
[result] => 175.911330049
)
)
如何获得具有最高结果值的公司的父密钥? 例如,一个返回键2的函数,因为这里的最大值是212.20
我尝试了这个,但它只返回maxium值而不是键。如何获得具有最大结果值的公司的父密钥?
function maxMarks($array) {
$max=0;
foreach ($array as $Rsult) {
$max=$Rsult['result']>$max ?$Rsult['result']:$max;
}
return $max;
}
echo maxMarks($result);
答案 0 :(得分:3)
尝试
function maxMarks($array) {
$max=0;
$max_key = '';
foreach ($array as $key=>$Rsult) {
$max = $Rsult['result'] > $max ? $Rsult['result'] : $max;
$max_key = $Rsult['result'] > $max ? $key : $max_key;
}
return $max_key;
}
echo maxMarks($result);