<?php
function change_top_items($array, $top_x) {
uasort($array, function($a, $b) {
if ($a == $b) return 0;
return ($a < $b) ? -1 : 1;
});
$compare = array_slice($array, -$top_x);
$modified = array_map(function($value) use ($compare) {
return in_array($value, $compare) ? 1 : 0;
}, $array);
ksort($modified);
return $modified;
}
$a = [5, 8, 2, 0, 1, 9, 1, 0, 5, 4];
print_r(change_top_items($a, 5));
preferences:
26.57 ms | 410 KiB | 5 Q