php 返回数组中最大值的索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1461348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return index of highest value in an array
提问by Jeff
From an array that looks something like the following, how can I get the index of the highest value in the array. For the array below, the desired result would be '11'.
从类似于以下内容的数组中,如何获取数组中最高值的索引。对于下面的数组,所需的结果是“11”。
Array (
[11] => 14
[10] => 9
[12] => 7
[13] => 7
[14] => 4
[15] => 6
)
回答by drAlberT
My solution is:
我的解决办法是:
$maxs = array_keys($array, max($array))
Note:
this way you can retrieve every keyrelated to a given max value.
注意:通过
这种方式,您可以检索与给定最大值相关的每个键。
If you are interested only in one key among allsimply use $maxs[0]
如果您只对其中一个键感兴趣,只需使用$maxs[0]
回答by Andrejs Cainikovs
<?php
$array = array(11 => 14,
10 => 9,
12 => 7,
13 => 7,
14 => 4,
15 => 6);
echo array_search(max($array), $array);
?>
array_search() return values:
array_search() 返回值:
Returns the key for needle if it is found in the array, FALSE otherwise.
如果在数组中找到,则返回针的键,否则返回 FALSE。
If needle is found in haystack more than once, the first matching keyis returned. To return the keys for all matching values, use array_keys() with the optional search_value parameter instead.
如果在 haystack 中多次找到 Needle,则返回第一个匹配的键。要返回所有匹配值的键,请改用 array_keys() 和可选的 search_value 参数。
回答by u231664
I know it's already answered but here is a solution I find more elegant:
我知道它已经得到了回答,但这里有一个我觉得更优雅的解决方案:
arsort($array);
reset($array);
echo key($array);
and voila!
瞧!
回答by luchaninov
Other answers may have shorter code but this one should be the most efficient and is easy to understand.
其他答案可能有更短的代码,但这应该是最有效的并且易于理解。
/**
* Get key of the max value
*
* @var array $array
* @return mixed
*/
function array_key_max_value($array)
{
$max = null;
$result = null;
foreach ($array as $key => $value) {
if ($max === null || $value > $max) {
$result = $key;
$max = $value;
}
}
return $result;
}
回答by xmatzx
My solution to get the higher key is as follows:
我获得更高键的解决方案如下:
max(array_keys($values['Users']));
回答by Aistina
Something like this should do the trick
像这样的事情应该可以解决问题
function array_max_key($array) {
$max_key = -1;
$max_val = -1;
foreach ($array as $key => $value) {
if ($value > $max_val) {
$max_key = $key;
$max_val = $value;
}
}
return $max_key;
}
回答by dnagirl
$newarr=arsort($arr);
$max_key=array_shift(array_keys($new_arr));
回答by Hanifeoglu
<?php
$array = array (
'11' => 14,
'10' => 9,
'12' => 7,
'13' => 7,
'14' => 4,
'15' => 6
);
foreach ($array as $key => $value) {
if ($value >= $max)
$max = max($array);
}
echo " The array in Maximum Value :".$max."<br/>";
?>
回答by Timur Asaliev
Function taken from http://www.php.net/manual/en/function.max.php
函数取自http://www.php.net/manual/en/function.max.php
function max_key($array) {
foreach ($array as $key => $val) {
if ($val == max($array)) return $key;
}
}
$arr = array (
'11' => 14,
'10' => 9,
'12' => 7,
'13' => 7,
'14' => 4,
'15' => 6
);
die(var_dump(max_key($arr)));
Works like a charm
奇迹般有效

