Navigation

 

Artikel Kategorien

This error is typical if you try to compare two arrays and one or both of them are multidimensional. The function array_diff expects only one dimensional arrays.

But what causes this error? The php internal function array_diff actually runs through the first level of the array and then tries to compare the elements with the identical operator “===” and try to force a convertion to string.

See the following example that causes this error:
$arr1 = ['key1' => ['level2' => 'value1']];
$arr1 = ['key1' => ['level2' => 'value2']];
var_dump(array_diff($arr1, $arr2));

 

When the function reaches the first element it will try to do the following:
if ((string) ['level2' => 'value1'] === (string) ['level2' => 'value2']) {
...

Because the PHP interpreter is not able to convert an array to a string, it will throw an exception/error.

 

Zuletzt aktualisiert am 28.03.2017