Skip to main content
The 2024 Developer Survey results are live! See the results
deleted 82 characters in body
Source Link
borrible
  • 17.3k
  • 7
  • 55
  • 77

If you just want the largest value in the array use the max function. This will return the largest value, although not the corresponding key. It does not change the original array.

If you care about the the key you could then do something like:

function max_key($array)
{
  $maximum$key = array_search(max($array);

  foreach, ($array as $key => $val)
    if ($val == $maximum) return $key;
}

(Edited to include @binaryLV's suggestion)

If you just want the largest value in the array use the max function. This will return the largest value, although not the corresponding key. It does not change the original array.

If you care about the the key you could do something like:

function max_key($array)
{
  $maximum = max($array);

  foreach ($array as $key => $val)
    if ($val == $maximum) return $key;
}

If you just want the largest value in the array use the max function. This will return the largest value, although not the corresponding key. It does not change the original array.

If you care about the the key you could then do

$key = array_search(max($array), $array)

(Edited to include @binaryLV's suggestion)

Source Link
borrible
  • 17.3k
  • 7
  • 55
  • 77

If you just want the largest value in the array use the max function. This will return the largest value, although not the corresponding key. It does not change the original array.

If you care about the the key you could do something like:

function max_key($array)
{
  $maximum = max($array);

  foreach ($array as $key => $val)
    if ($val == $maximum) return $key;
}