Array find min value with user-defined comparison compare function My user defined comparison compare function is defined with an array:

$boardkey_to_values=Array(19=&gt;2601248,23=&gt;2601248,39=&gt;2603445,
    43=&gt;2256319,47=&gt;2632006,59=&gt;2603445,63=&gt;2232152,67=&gt;2260713,71=&gt;2632006,...)


so 23&gt;19 EDIT:23=19, 39&gt;19, 39&lt;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.

$min_key=Array(0);
foreach ($board_id as $key =&gt; $value) {
if ($boardkey_to_values[$board_id[$min_key[0]]]&gt;$boardkey_to_values[$value])
    $min_key=Array($key);
if ($boardkey_to_values[$board_id[$min_key[0]]]==$boardkey_to_values[$value] &amp;&amp; $min_key[0]!=$key)
    $min_key[]=$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])
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);
asort($mapped);
list($min_value, $id) = reset($mapped);
$min_ids = array($id);
while (list($value, $id) = next($mapped)) {
    if ($value == $min_value) {
        $min_ids[] = $id;
    }
    else {
        break;
    }
}


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