3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array1 = ['keepThis', 'remove', 'keep', 'save', 'delete', 'extra']; $array2 = ['hangOnto', 'remove', 'retain', 'keep', 'delete']; $clean1 = $array1; $clean2 = array_diff_ukey( $array2, $array1, // Need to have "&" so the values get set. function($a, $b) use (&$clean1, $array1, $array2) { // Use isset() just in case keys are not identical // or arrays are not the same length. if (isset($array2[$b]) && $array2[$b] === $array1[$b]) { unset($clean1[$b]); } return strcmp($array2[$a], $array1[$b]); }); print_r($clean1); print_r($clean2);
Output for 7.4.0 - 7.4.30, 8.0.1 - 8.0.22, 8.1.0 - 8.1.9
Array ( [0] => keepThis [2] => keep [3] => save [5] => extra ) Array ( [0] => hangOnto [2] => retain )

preferences:
27.06 ms | 409 KiB | 5 Q