1

I need to get all maximum values from array using php.

For this array:

$arr = array('a'=>10,'b'=>20,'c'=>5,'d'=>20);

I used below code,

$key = array_search(max($arr), $arr);

but I get only b, I need to get both b and d -- all keys with the highest value.

Joshua
  • 40,822
  • 8
  • 72
  • 132
Thiyagu
  • 746
  • 3
  • 11
  • 29
  • 1
    Possible duplicate of [Return index of highest value in an array](https://stackoverflow.com/questions/1461348/return-index-of-highest-value-in-an-array) – Nawin Jun 29 '17 at 13:06
  • 2
    Possible duplicate of [PHP Get multiple highest values from array](https://stackoverflow.com/questions/21484765/php-get-multiple-highest-values-from-array) – mickmackusa Jun 29 '17 at 13:28

1 Answers1

6

To find all keys use array_keys with a second parameter:

$arr = array('a'=>10,'b'=>20,'c'=>5,'d'=>20);
$key = array_keys($arr, max($arr));

By the way it is said on array_search man page)

u_mulder
  • 54,101
  • 5
  • 48
  • 64