102

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'.

Array (
    [11] => 14
    [10] => 9
    [12] => 7
    [13] => 7
    [14] => 4
    [15] => 6
)
Jeff
  • 2,794
  • 8
  • 30
  • 35
  • 1
    It's been a while, but your array already seems sorted (descending); either it's a bad example or you really just need `reset($arr); echo key($arr);` :) – Ja͢ck Sep 26 '13 at 05:56

8 Answers8

236

My solution is:

$maxs = array_keys($array, max($array))

Note:
this way you can retrieve every key related to a given max value.

If you are interested only in one key among all simply use $maxs[0]

drAlberT
  • 22,059
  • 5
  • 34
  • 40
  • How to see if two values are same? – talha2k Feb 13 '13 at 12:07
  • you can check the result if has two or more values if has duplicates – Julio Popócatl Jul 26 '13 at 06:27
  • @AlphaMale you mean two keys I suppose, as the max value is only one by definition .. @JustinE if you don't want duplicate keys simply search the max (ie avoid `array_keys`) and you'll get only one key corresponding to the max value – drAlberT Dec 18 '14 at 10:15
48
<?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:

Returns the key for needle if it is found in the array, FALSE otherwise.

If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use array_keys() with the optional search_value parameter instead.

Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
12

I know it's already answered but here is a solution I find more elegant:

arsort($array);
reset($array);
echo key($array);

and voila!

David 天宇 Wong
  • 3,724
  • 4
  • 35
  • 47
5

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;
}
luchaninov
  • 6,792
  • 6
  • 60
  • 75
1

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;
}
Aistina
  • 12,435
  • 13
  • 69
  • 89
1

My solution to get the higher key is as follows:

max(array_keys($values['Users']));
mtk
  • 13,221
  • 16
  • 72
  • 112
xmatzx
  • 49
  • 5
0
$newarr=arsort($arr);
$max_key=array_shift(array_keys($new_arr));
dnagirl
  • 20,196
  • 13
  • 80
  • 123
-8

Function taken from 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

  • 8
    Not to speak about performance. Foreaching through array, checking max value every time is even worse than "bad practice". – bisko Sep 22 '09 at 17:42
  • 1
    I mentioned its not my implementation. It was a quick and dirty copy/paste that the OP obviously couldn't do himself, mister. – Timur Asaliev Sep 22 '09 at 19:25
  • 3
    Not going to lie, you made me chuckle a bit. You're concerned about max() for each iteration through the array? It's "worse than bad practice". No, I don't believe it is. It's not the most elegant, but it works. – Sean Feb 20 '15 at 10:51