4

Say you have this array:

$users = [
    'a' => 2,
    'b' => 1,
    'c' => 1,
    'd' => 3
];

I need to get the keys with the lowest values. So in this case that would be b and c.

Currently doing it like this:

asort($users);

$lowestValue = array_values($users)[0];

foreach ($users as $k => $v)
    if ($v == $lowestValue)
        $lowestUsers[$k] = $v;

print_r($lowestUsers);

This works fine but is there a more shorter/efficient way of doing this?

5 Answers 5

6

You can use array_keys to find the keys which have the lowest values in the array. If you pass array_keys a value as the second parameter it will return an array of all the keys which have that value in the array. Note that just using min is probably the simplest way to get the lowest value:

$lowestValue = min($users);
print_r(array_keys($users, $lowestValue));

Output:

Array (
  [0] => b
  [1] => c 
)

Demo on 3v4l.org

If you actually want the elements of the $users array which have the lowest value, you can take the output of array_keys and run it through array_intersect_key:

$lowestValue = min($users);
$lowestValueKeys = array_keys($users, $lowestValue);
$lowestUsers = array_intersect_key($users, array_flip($lowestValueKeys));
print_r($lowestUsers);

Although if that is the case it's simpler just to use array_filter:

$lowestValue = min($users);
$lowestUsers = array_filter($users, function ($v) use ($lowestValue) { return $v === $lowestValue; });
print_r($lowestUsers);

Output in both cases is

Array (
  [b] => 1
  [c] => 1 
)

Demo on 3v4l.org

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

Comments

2

You can do this by the combination of array_keys and min. There is no need of sorting or doing loops,

print_r(array_keys($users, min($users)));

Output

Array
(
    [0] => b
    [1] => c
)

Working demo.

Comments

1

This one helps you.

$users = [
    'a' => 2,
    'b' => 1,
    'c' => 1,
    'd' => 3
];

echo array_search(min($users ), $users );

1 Comment

array_search only returns the first matching key. OP wants to find all keys with the lowest value
1

You can use min() function to find the lowest value in an array and array_filter() function to filtering data. For example:

$users = [
    'a' => 2,
    'b' => 1,
    'c' => 1,
    'd' => 3
];

$lowest = min($users);

$result = array_filter($users, function($value, $key) use ($lowest) {
    return $value === $lowest;
}, ARRAY_FILTER_USE_BOTH);

result:

Array ( [b] => 1 [c] => 1 )

Comments

0

Your have more options:

  1. Use min() to get the minimum value and array_keys($users, $minvalue) to get related key (include duplicates)
  2. Sort the array by using asort($users) and get minimum value with $users[0] (not showing duplicates)

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.