3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array1 = ['keepThis', 'remove', false, 'keep', 'save', 'delete', 'extra']; $array2 = ['hangOnto', 'remove', null, '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 8.1.0 - 8.1.9
Deprecated: strcmp(): Passing null to parameter #1 ($string1) of type string is deprecated in /in/np0ag on line 20 Deprecated: strcmp(): Passing null to parameter #1 ($string1) of type string is deprecated in /in/np0ag on line 20 Deprecated: strcmp(): Passing null to parameter #1 ($string1) of type string is deprecated in /in/np0ag on line 20 Array ( [0] => keepThis [2] => [3] => keep [4] => save [6] => extra ) Array ( [0] => hangOnto [3] => retain )
Output for 7.4.0 - 7.4.30, 8.0.1 - 8.0.22
Array ( [0] => keepThis [2] => [3] => keep [4] => save [6] => extra ) Array ( [0] => hangOnto [3] => retain )

preferences:
18.77 ms | 411 KiB | 5 Q