When running your script in a modern php environment, the Warnings should indicate that you are using the wrong tool for the job.
Bad Code: (Demo)
$array1 = [[12 => 'new q sets'], [11 => 'common set']];
$array2 = [[11 => 'common set']];
var_export(array_diff_assoc($array1, $array2));
Bad Output:
Warning: Array to string conversion in /in/jIUcq on line 6
Warning: Array to string conversion in /in/jIUcq on line 6
array (
1 =>
array (
11 => 'common set',
),
)
You don't actually want to compare the first level indexes anyhow because related/matching rows may have different first level indexes.
Instead, you should use array_udiff()
to compare the associative rows (and ignore the first level keys). Making a 3-way comparison -- as array_udiff()
expects from the callback -- without iterated function calls is possible with the "spaceship operator". In the below snippet, $a
and $b
represent rows of data.
Proper Code: (Demo)
var_export(
array_udiff($array1, $array2, fn($a, $b) => $a <=> $b)
);
Proper Output:
array (
0 =>
array (
12 => 'new q sets',
),
)