3v4l.org

run code in 300+ PHP versions simultaneously
<?php $src = ["apple", "cherry", "grape", "lemon", "orange", "strawberry", "cherry", "cherry"]; $dst = ["apple", "banana", "cherry", "orange", "pear", "cherry"]; $result = []; while ($src && $dst) { if ($src[0] === $dst[0]) { // align identical values $result[] = [array_shift($src), array_shift($dst)]; continue; } $earliestSrcMatch = array_search($src[0], $dst); $earliestDstMatch = array_search($dst[0], $src); if ($earliestSrcMatch === $earliestDstMatch) { //both false (not 0) $result[] = [array_shift($src), null]; $result[] = [null, array_shift($dst)]; continue; } elseif ( $earliestDstMatch === false || ( $earliestSrcMatch !== false && $earliestSrcMatch < $earliestDstMatch ) ) { while ($dst && $src[0] !== $dst[0]) { $result[] = [null, array_shift($dst)]; } } elseif ( $earliestSrcMatch === false || $earliestSrcMatch > $earliestDstMatch ) { while ($src && $src[0] !== $dst[0]) { $result[] = [array_shift($src), null]; } } } while ($src) { $result[] = [array_shift($src), null]; } while ($dst) { $result[] = [null, array_shift($dst)]; } echo "<table border=1> "; foreach ($result as $i => [$s, $d]) { printf( "<tr> <td>%d</td> <td>%s</td> <td>%s</td> </tr>\n", ++$i, $s, $d ); } echo "</table>";

preferences:
112.72 ms | 406 KiB | 5 Q