3v4l.org

run code in 300+ PHP versions simultaneously
<?php function permuteUnique($items, $perms = [], &$return = []) { if (empty($items)) { $return[] = $perms; } else { sort($items); $prev = false; for ($i = count($items) - 1; $i >= 0; --$i) { $newitems = $items; $tmp = array_splice($newitems, $i, 1)[0]; if ($tmp != $prev) { $prev = $tmp; $newperms = $perms; array_unshift($newperms, $tmp); permuteUnique($newitems, $newperms, $return); } } return $return; } } $permutations = permuteUnique([1, 2, 2, 3, 4, 4, 4, 4]); var_dump($permutations);

preferences:
39.94 ms | 402 KiB | 5 Q