Return index of highest value in an array

PhpArrays

Php Problem Overview


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
)

Php Solutions


Solution 1 - Php

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]

Solution 2 - Php

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

Solution 3 - Php

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

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

and voila!

Solution 4 - Php

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

Solution 5 - Php

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

Solution 6 - Php

My solution to get the higher key is as follows:

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

Solution 7 - Php

$newarr=arsort($arr);
$max_key=array_shift(array_keys($new_arr));

Solution 8 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeffView Question on Stackoverflow
Solution 1 - PhpdrAlberTView Answer on Stackoverflow
Solution 2 - PhpAndrejs CainikovsView Answer on Stackoverflow
Solution 3 - PhpDavid 天宇 WongView Answer on Stackoverflow
Solution 4 - PhpluchaninovView Answer on Stackoverflow
Solution 5 - PhpAistinaView Answer on Stackoverflow
Solution 6 - PhpxmatzxView Answer on Stackoverflow
Solution 7 - PhpdnagirlView Answer on Stackoverflow
Solution 8 - PhpTimur AsalievView Answer on Stackoverflow