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));
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.20, 7.3.32 - 7.3.33
array(3) { [0]=> int(5) [1]=> int(10) [2]=> int(105) }
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /in/P5JdW on line 10 array(3) { [0]=> int(5) [1]=> int(10) [2]=> int(105) }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.34, 8.3.0 - 8.3.35, 8.4.1 - 8.4.26, 8.5.0 - 8.5.11
Fatal error: Uncaught Error: Call to undefined function each() in /in/P5JdW:10 Stack trace: #0 /in/P5JdW(46): array_udiff_userland(Object(Closure), Array, Array) #1 {main} thrown in /in/P5JdW on line 10
Process exited with code 255.

preferences:
35.19 ms | 1151 KiB | 15 Q