Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My user defined comparison compare function is defined with an array:


so 23>19 EDIT:23=19, 39>19, 39<23,...

I have an array of values $board_id, and I want to get the array of all the keys for the minimum values.

Here's my solution, but I don't think it is really intuitive.

foreach ($board_id as $key => $value) {
if ($boardkey_to_values[$board_id[$min_key[0]]]>$boardkey_to_values[$value])
if ($boardkey_to_values[$board_id[$min_key[0]]]==$boardkey_to_values[$value] && $min_key[0]!=$key)

Here is an example to show how this should behave:

say $board_id = [19,23,39,47,71]

it will look at the values corresponding to this numbers [2601248,2601248,2603445,2632006,2632006]

it looks for the smallest value: 2601248

so the result ($min_key should be [19,23])

share|improve this question
Why is 23 > 19 when they both map to the same value? Shouldn't they be equal? – David Harkness Mar 26 '13 at 22:00
Yes you're right 23=19, I have edited my question – edi9999 Mar 27 '13 at 7:16
up vote 1 down vote accepted

This should be O(n)* where n is count($board_id).

$mapped = array_map(function ($id) use ($boardkey_to_values) {
    return array($boardkey_to_values[$id], $id);
}, $board_id);
list($min_value, $id) = reset($mapped);
$min_ids = array($id);
while (list($value, $id) = next($mapped)) {
    if ($value == $min_value) {
        $min_ids[] = $id;
    else {

* ignoring the call to asort which is probably O(n log n).

share|improve this answer
What exactly does the use keyword do after the parameters of an anonymous function ? – edi9999 Mar 27 '13 at 7:27
It allows the anonymous function to access the named variable. Without it the function can only access the local variables and arguments it declares. – David Harkness Mar 27 '13 at 7:55

You could try doing it this way:

$min_key = null;

foreach(array_count_values($boardkey_to_values) as $val) {
  $min_key = array_slice( array_keys($boardkey_to_values), $val );

I believe that would accomplish what you're trying to do.

share|improve this answer
Your function is very slow compared to mine, because array_slice is slow given the size of the $boardkey_to_values (more than 40 000 entries). And it isn't exactly what I meant (cf edit for the example) – edi9999 Feb 24 '13 at 21:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.