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.

2

1 Answer 1

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)

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

Comments

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.