<?php $array = [ "Jacob" => 5, "bob" => 20, "John" => 10, "Jane" => 20, "Jill" => 15, ]; $limit = 3; $result = []; foreach ($array as $k => $v) { if (isset($result[$v])) { $result[$v][$k] = $v; } elseif (count($result) < $limit) { $result[$v][$k] = $v; } else { $min = min(array_keys($result)); if ($v > $min) { $result[$v][$k] = $v; unset($result[$min]); } } } krsort($result); var_export(array_values($result));
You have javascript disabled. You will not be able to edit any code.