Why array_diff() gives Array to string conversion error?

半腔热情 提交于 2019-11-30 16:46:43

According to it:

php -r 'array_diff(array("a" => array("b" => 4)), array(1));'
PHP Notice:  Array to string conversion in Command line code on line 1
PHP Stack trace:
PHP   1. {main}() Command line code:0
PHP   2. array_diff() Command line code:1

One of your arrays is multidimensional.

array_diff only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);

Yes, the strict answer is because "One of your arrays is multidimensional."

Another useful note might be - depending on your needs of further parsing the actual differences - consider first testing your arrays with:

$diff = strcmp(json_encode($stockist), json_encode($arr));

or

$diff = strspn(json_encode($stockist) ^ json_encode($arr), "\0");

or

$diff = xdiff_string_diff(json_encode($stockist), json_encode($arr));

All these options will compare the entire array tree, not just the top level.

kenorb

Since array_diff can only deals with one dimension, you can either:

According to PHP documentation for the function

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.

For more information refer to http://php.net/manual/en/function.array-diff.php

You can see in the array_diff() documentation that:

Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In other words: when the string representation is the same.

So it looks like you can't use this function with multi dimensional array, or in fact any value that cannot be converted to a string. This is because the function will cast values to a string to do the comparison.

You can write your own function to recursively check arrays for a difference - in fact the following is from the comments of the docs linked above.

You can see the comment here.

function arrayRecursiveDiff($aArray1, $aArray2) { 
    $aReturn = array(); 

    foreach ($aArray1 as $mKey => $mValue) { 
        if (array_key_exists($mKey, $aArray2)) { 
            if (is_array($mValue)) { 
                $aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]); 
                if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; } 
            } else { 
                if ($mValue != $aArray2[$mKey]) { 
                    $aReturn[$mKey] = $mValue; 
                } 
            } 
        } else { 
            $aReturn[$mKey] = $mValue; 
        } 
    } 

    return $aReturn; 
}

I've got the same error and found the following bug report for php:

https://bugs.php.net/bug.php?id=60198

Some of the array_* functions that compare elements in multiple arrays do so by (string)$elem1 === (string)$elem2.

If $elem1 or $elem2 is an array, then the array to string notice is thrown.

Two examples of functions that can throw this are array_intersect() and array_diff().

If these functions are not expected to take arrays with other arrays as values, this should be mentioned on the documentation pages.

That report describes, why php throws an error on comparing a multi-dimensional array.

What about my solution:

$diff = strcmp(serialize($arr1), serialize($arr2))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!