array_diff_assoc, array_diff and array_unique warn on arrays of arrays
Fix it fast
Most common cause, 1 of 3array_diff_assoc compares a record that holds nested arrays
Compare values with a callback.
<?php
$old = ['name' => 'Ana', 'tags' => ['php', 'sql']];
$new = ['name' => 'Ana', 'tags' => ['php']];
$changed = array_udiff_assoc($old, $new, fn ($a, $b) => $a <=> $b);
// ['tags' => ['php', 'sql']]
function diff_recursive(array $a, array $b): array {
$out = [];
foreach ($a as $key => $value) {
if (!array_key_exists($key, $b)) { $out[$key] = $value; continue; }
if (is_array($value) && is_array($b[$key])) {
$sub = diff_recursive($value, $b[$key]);
if ($sub !== []) { $out[$key] = $sub; }
} elseif ($value !== $b[$key]) {
$out[$key] = $value;
}
}
return $out;
}
// diff_recursive($old, $new) gives ['tags' => [1 => 'sql']]Short answer
Warning: Array to string conversion from array_diff_assoc(), array_diff() or array_unique() means the arrays hold arrays. These functions compare values by their string form, so every nested array turns into the text Array: PHP warns and treats different rows as equal. Use array_udiff_assoc with <=>, compare rows by an ID, or pass SORT_REGULAR to array_unique. Applies to PHP 8.2-8.6.
Which cause do you have?
Open the line the warning names and list which values in each compared array are arrays themselves. Nested values inside two records point to cause 1, two lists of rows to cause 2, and a warning on an array_unique line to cause 3.
var_dump(array_keys(array_filter($old, 'is_array')));
var_dump(array_keys(array_filter($new, 'is_array')));Then find the line you see in this list and open its fix:
- 1array_diff_assoc compares a record that holds nested arrays
The warning points at array_diff_assoc, and the compared records have keys such as tags or address whose values are arrays - 2array_diff or array_intersect gets two lists of rows
Both arguments are lists like [['id' => 1, ...], ['id' => 2, ...]], the warning repeats, and the result is empty - 3array_unique is run on a list of arrays
The warning points at array_unique, and the returned list has shrunk to a single row
What does the error look like?
Warning: Array to string conversionarray_diff_assoc() returns an empty array although a nested value changedarray_unique() keeps only the first row of a list of arrays
Cause 1 of 3
array_diff_assoc compares a record that holds nested arrays
You seeThe warning points at array_diff_assoc, and the compared records have keys such as tags or address whose values are arrays
Check
Array to string conversion on an array_diff_assoc line means at least one compared value is an array. The PHP manual defines two values as equal only when (string) $elem1 === (string) $elem2 and says the function checks one dimension only. Each nested array is cast to the same text, Array, on both sides, so PHP warns and the changed nested value is left out of the result.
grep -rn "array_diff_assoc(" src/Fix
Compare values with a callback. The spaceship operator compares two arrays member by member, so array_udiff_assoc reports a changed nested value. When you need only the changed leaves, walk the arrays yourself.
<?php
$old = ['name' => 'Ana', 'tags' => ['php', 'sql']];
$new = ['name' => 'Ana', 'tags' => ['php']];
$changed = array_udiff_assoc($old, $new, fn ($a, $b) => $a <=> $b);
// ['tags' => ['php', 'sql']]
function diff_recursive(array $a, array $b): array {
$out = [];
foreach ($a as $key => $value) {
if (!array_key_exists($key, $b)) { $out[$key] = $value; continue; }
if (is_array($value) && is_array($b[$key])) {
$sub = diff_recursive($value, $b[$key]);
if ($sub !== []) { $out[$key] = $sub; }
} elseif ($value !== $b[$key]) {
$out[$key] = $value;
}
}
return $out;
}
// diff_recursive($old, $new) gives ['tags' => [1 => 'sql']]Verify
Run the script with every error level shown: no warning appears, and the result now lists the changed nested key.
php -d error_reporting=-1 -d display_errors=1 sync.phpConfirmed by php.net
Cause 2 of 3
array_diff or array_intersect gets two lists of rows
You seeBoth arguments are lists like [['id' => 1, ...], ['id' => 2, ...]], the warning repeats, and the result is empty
Check
array_diff raising Array to string conversion over two lists of database rows is the same string rule at work. The manual says two elements are equal only when their string forms are identical, and every row reads as Array, so all rows look alike and nothing is reported as missing.
grep -rn "array_diff(\|array_intersect(" src/Fix
Compare the rows by the column that identifies them, with array_udiff and a callback, or by collecting the IDs of one side with array_column.
<?php
$removed = array_udiff($saved, $sent, fn ($a, $b) => $a['id'] <=> $b['id']);
// same result, readable for large lists
$sentIds = array_column($sent, 'id');
$removed = array_filter($saved, fn ($row) => !in_array($row['id'], $sentIds, true));Verify
The result contains exactly the rows whose id is absent from the second list, and the run prints no warning.
php -d error_reporting=-1 -d display_errors=1 sync.phpConfirmed by php.net
Cause 3 of 3
array_unique is run on a list of arrays
You seeThe warning points at array_unique, and the returned list has shrunk to a single row
Check
array_unique compares items as strings by default and its manual notes it is not intended for multi-dimensional arrays. Every row becomes Array, every row therefore counts as a duplicate of the first, and the function returns one row while printing the warning.
grep -rn "array_unique(" src/Fix
Pass SORT_REGULAR so rows are compared as arrays. For rows with mixed numeric and text strings the manual warns that SORT_REGULAR can leave duplicates, so de-duplicate by a key column with array_column instead.
<?php
$unique = array_unique($rows, SORT_REGULAR);
// or keep one row per id value
$uniqueById = array_values(array_column($rows, null, 'id'));Verify
Both versions return one row per distinct record, and the warning is gone.
php -d error_reporting=-1 -d display_errors=1 dedupe.phpConfirmed by php.net
How do I stop it coming back?
- Turn warnings into exceptions in the test bootstrap, so a set function that silently compares Array to Array fails the suite instead of passing.
- Diff records by their key column, never by whole rows, when data comes from a database or an API.
- Keep one unit test where only a nested value differs, which proves the diff code looks below the first level.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Spotless Laundry, EZ Estimater.