3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array1 = ['keepThis', 'remove', false, 'keep', 'save', 'delete']; $array2 = ['hangOnto', 'remove', null, 'retain', 'keep', 'delete', 'extra']; $clean1 = $array1; $clean2 = array_diff_ukey( $array2, $array1, // Need to have "&" so the values get set. function($a, $b) use (&$clean1, $array1, $array2) { echo "a = $a, b = $b\n"; // 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
a = 0, b = 1 a = 1, b = 2 a = 0, b = 2 a = 1, b = 3 a = 0, b = 3 a = 1, b = 4 a = 4, b = 5 a = 1, b = 5 a = 3, b = 5 a = 0, b = 5 a = 2, b = 5 Deprecated: strcmp(): Passing null to parameter #1 ($string1) of type string is deprecated in /in/V5gYq on line 20 a = 4, b = 6 Warning: Undefined array key 6 in /in/V5gYq on line 16 Warning: Undefined array key 6 in /in/V5gYq on line 20 Deprecated: strcmp(): Passing null to parameter #2 ($string2) of type string is deprecated in /in/V5gYq on line 20 a = 3, b = 6 Warning: Undefined array key 6 in /in/V5gYq on line 16 Warning: Undefined array key 6 in /in/V5gYq on line 20 Deprecated: strcmp(): Passing null to parameter #2 ($string2) of type string is deprecated in /in/V5gYq on line 20 a = 5, b = 6 Warning: Undefined array key 6 in /in/V5gYq on line 16 Warning: Undefined array key 6 in /in/V5gYq on line 20 Deprecated: strcmp(): Passing null to parameter #2 ($string2) of type string is deprecated in /in/V5gYq on line 20 a = 6, b = 2 a = 0, b = 1 a = 1, b = 2 a = 0, b = 2 a = 1, b = 3 a = 0, b = 3 a = 1, b = 4 a = 4, b = 5 a = 1, b = 5 a = 3, b = 5 a = 0, b = 5 a = 2, b = 5 Deprecated: strcmp(): Passing null to parameter #1 ($string1) of type string is deprecated in /in/V5gYq on line 20 a = 2, b = 2 Deprecated: strcmp(): Passing null to parameter #1 ($string1) of type string is deprecated in /in/V5gYq on line 20 a = 6, b = 2 a = 6, b = 5 a = 6, b = 0 a = 6, b = 3 a = 6, b = 1 a = 6, b = 4 a = 5, b = 2 a = 5, b = 5 a = 0, b = 2 a = 0, b = 5 a = 0, b = 0 a = 0, b = 3 a = 0, b = 1 a = 0, b = 4 a = 3, b = 2 a = 3, b = 5 a = 3, b = 0 a = 3, b = 3 a = 3, b = 1 a = 3, b = 4 a = 1, b = 2 a = 1, b = 5 a = 1, b = 0 a = 1, b = 3 a = 1, b = 1 a = 4, b = 2 a = 4, b = 5 a = 4, b = 0 a = 4, b = 3 Array ( [0] => keepThis [2] => [3] => keep [4] => save ) Array ( [0] => hangOnto [3] => retain [6] => extra )
Output for 8.0.1 - 8.0.22
a = 0, b = 1 a = 1, b = 2 a = 0, b = 2 a = 1, b = 3 a = 0, b = 3 a = 1, b = 4 a = 4, b = 5 a = 1, b = 5 a = 3, b = 5 a = 0, b = 5 a = 2, b = 5 a = 4, b = 6 Warning: Undefined array key 6 in /in/V5gYq on line 16 Warning: Undefined array key 6 in /in/V5gYq on line 20 a = 3, b = 6 Warning: Undefined array key 6 in /in/V5gYq on line 16 Warning: Undefined array key 6 in /in/V5gYq on line 20 a = 5, b = 6 Warning: Undefined array key 6 in /in/V5gYq on line 16 Warning: Undefined array key 6 in /in/V5gYq on line 20 a = 6, b = 2 a = 0, b = 1 a = 1, b = 2 a = 0, b = 2 a = 1, b = 3 a = 0, b = 3 a = 1, b = 4 a = 4, b = 5 a = 1, b = 5 a = 3, b = 5 a = 0, b = 5 a = 2, b = 5 a = 2, b = 2 a = 6, b = 2 a = 6, b = 5 a = 6, b = 0 a = 6, b = 3 a = 6, b = 1 a = 6, b = 4 a = 5, b = 2 a = 5, b = 5 a = 0, b = 2 a = 0, b = 5 a = 0, b = 0 a = 0, b = 3 a = 0, b = 1 a = 0, b = 4 a = 3, b = 2 a = 3, b = 5 a = 3, b = 0 a = 3, b = 3 a = 3, b = 1 a = 3, b = 4 a = 1, b = 2 a = 1, b = 5 a = 1, b = 0 a = 1, b = 3 a = 1, b = 1 a = 4, b = 2 a = 4, b = 5 a = 4, b = 0 a = 4, b = 3 Array ( [0] => keepThis [2] => [3] => keep [4] => save ) Array ( [0] => hangOnto [3] => retain [6] => extra )
Output for 7.4.0 - 7.4.30
a = 0, b = 1 a = 1, b = 2 a = 0, b = 2 a = 1, b = 3 a = 0, b = 3 a = 1, b = 4 a = 4, b = 5 a = 1, b = 5 a = 3, b = 5 a = 0, b = 5 a = 2, b = 5 a = 4, b = 6 Notice: Undefined offset: 6 in /in/V5gYq on line 16 Notice: Undefined offset: 6 in /in/V5gYq on line 20 a = 3, b = 6 Notice: Undefined offset: 6 in /in/V5gYq on line 16 Notice: Undefined offset: 6 in /in/V5gYq on line 20 a = 5, b = 6 Notice: Undefined offset: 6 in /in/V5gYq on line 16 Notice: Undefined offset: 6 in /in/V5gYq on line 20 a = 6, b = 2 a = 0, b = 1 a = 1, b = 2 a = 0, b = 2 a = 1, b = 3 a = 0, b = 3 a = 1, b = 4 a = 4, b = 5 a = 1, b = 5 a = 3, b = 5 a = 0, b = 5 a = 2, b = 5 a = 2, b = 2 a = 6, b = 2 a = 6, b = 5 a = 6, b = 0 a = 6, b = 3 a = 6, b = 1 a = 6, b = 4 a = 5, b = 2 a = 5, b = 5 a = 0, b = 2 a = 0, b = 5 a = 0, b = 0 a = 0, b = 3 a = 0, b = 1 a = 0, b = 4 a = 3, b = 2 a = 3, b = 5 a = 3, b = 0 a = 3, b = 3 a = 3, b = 1 a = 3, b = 4 a = 1, b = 2 a = 1, b = 5 a = 1, b = 0 a = 1, b = 3 a = 1, b = 1 a = 4, b = 2 a = 4, b = 5 a = 4, b = 0 a = 4, b = 3 Array ( [0] => keepThis [2] => [3] => keep [4] => save ) Array ( [0] => hangOnto [3] => retain [6] => extra )

preferences:
22.97 ms | 417 KiB | 5 Q