3v4l.org

run code in 500+ PHP versions simultaneously
<?php function array_udiff_userland(callable $cb, array $left, array $right) { usort($left, $cb); usort($right, $cb); reset($right); $result = []; $cur = each($right); foreach ($left as $el) { if ($cur === false) { // if we are already at the end of $right list //(it could be because $right was empty or if we reached its end in the iterations) $result[] = $el; continue; } $dif = $cb($el, $cur[1]); if ($dif > 0) { // $el > $cur - we need to advance $cur further until we find equal or greater element while ($dif > 0 && ($cur = each($right)) !== false) { $dif = $cb($el, $cur[1]); } if ($cur === false) { // $right list has reached its end $result[] = $el; continue; } } if ($dif === 0) { // elements are the same - we skip the current element continue; } if ($dif < 0) { $result[] = $el; } } return $result; } $l = [15, 5, 10, 90, 105]; $r = [6, 9, 15, 15, 100, 90, 400]; $cb = function ($l, $r) { return $l - $r; }; var_dump(array_udiff_userland($cb, $l, $r));

preferences:
20.46 ms | 476 KiB | 4 Q