105

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
)
1
  • 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); :) Commented Sep 26, 2013 at 5:56

8 Answers 8

241

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]

Sign up to request clarification or add additional context in comments.

3 Comments

How to see if two values are same?
you can check the result if has two or more values if has duplicates
@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
52
<?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.

Comments

13

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

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

and voila!

1 Comment

the reset is not necessary, btw.
6

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;
}

Comments

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;
}

1 Comment

Better use the first item’s key and value as the default value.
0
$newarr=arsort($arr);
$max_key=array_shift(array_keys($new_arr));

Comments

0

My solution to get the higher key is as follows:

max(array_keys($values['Users']));

Comments

-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

3 Comments

Not to speak about performance. Foreaching through array, checking max value every time is even worse than "bad practice".
I mentioned its not my implementation. It was a quick and dirty copy/paste that the OP obviously couldn't do himself, mister.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.