Bug #60198 [Com]: Array to string notice from array functions
| From: | chx1975 at gmail dot com | Date: | Fri, 11 Nov 2011 00:53:18 +0000 |
| Subject: | Bug #60198 [Com]: Array to string notice from array functions | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-165028@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=60198&edit=1
ID: 60198
Comment by: chx1975 at gmail dot com
Reported by: simon at simon dot geek dot nz
Summary: Array to string notice from array functions
Status: Bogus
Type: Bug
Package: *General Issues
PHP Version: 5.4SVN-2011-11-02 (SVN)
Block user comment: N
Private report: N
New Comment:
This should be changed to a documentation bug because up until now if you had
outside knowledge that those arrays are the same (for eg they are empty) then
this simply worked. If you decided to break it, then it needs to be documented
as a change because from a user perspective it is one.
Previous Comments:
------------------------------------------------------------------------
[2011-11-11 00:51:23] rasmus@php.net
The problem here is that these functions aren't recursive. They just go one
level deep and assume arrays of scalars. This means that the notice is a good
idea because the result when you pass it nested arrays is likely not the one you
wanted.
For example. If we change your code example slightly:
$left = [ 1, 2, [3] ];
$right = [2, 'Array'];
What should the intersection of these two be? Surely just the '2' since there is
no nested array with a 3 in $right and there is no string 'Array' in $left. But
the actual output is: [ 2, [3] ]
Oops! That's not what we expected and that is why this result gives you a notice
telling you that the result is likely flawed because of an array to string
conversion. Now, it might be nice if array_intersect was smarter and could
handle nested arrays, but that would be a different feature enhancement bug.
It also looks like this is well-documented. The array_intersect() doc page has
this highlighted note:
Note: Two elements are considered equal if and only if (string) $elem1 ===
(string) $elem2. In words: when the string representation is the same.
------------------------------------------------------------------------
[2011-11-11 00:05:29] chx1975 at gmail dot com
This affects the Drupal project. All our tests are failing because of this bug.
We see it with array_intersect_assoc().
------------------------------------------------------------------------
[2011-11-02 08:33:27] simon at simon dot geek dot nz
Description:
------------
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.
Test script:
---------------
<?php
$left = array('b', array('a'));
$right = array('a', 'd');
print_r(array_intersect($left, $right));
print_r(array_diff($left, $right));
Expected result:
----------------
Array
(
)
Array
(
[0] => b
[1] => Array
(
[0] => a
)
)
Actual result:
--------------
PHP Notice: Array to string conversion in /Users/simon/test.php on line 4
PHP Notice: Array to string conversion in /Users/simon/test.php on line 4
PHP Notice: Array to string conversion in /Users/simon/test.php on line 5
PHP Notice: Array to string conversion in /Users/simon/test.php on line 5
PHP Notice: Array to string conversion in /Users/simon/test.php on line 5
Notice: Array to string conversion in /Users/simon/test.php on line 4
Notice: Array to string conversion in /Users/simon/test.php on line 4
Array
(
)
Notice: Array to string conversion in /Users/simon/test.php on line 5
Notice: Array to string conversion in /Users/simon/test.php on line 5
Notice: Array to string conversion in /Users/simon/test.php on line 5
Array
(
[0] => b
[1] => Array
(
[0] => a
)
)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60198&edit=1