php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60198 Array to string notice from array functions
Submitted: 2011-11-02 08:33 UTC Modified: 2011-11-11 00:51 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:2 of 3 (66.7%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: simon at simon dot geek dot nz Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.4SVN-2011-11-02 (SVN) OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: simon at simon dot geek dot nz
New email:
PHP Version: OS:

 

 [2011-11-02 08:33 UTC] 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
        )

)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-11-11 00:05 UTC] 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-11 00:51 UTC] 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:51 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2011-11-11 00:53 UTC] chx1975 at gmail dot com
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.
 [2012-03-09 14:11 UTC] taneli at crasman dot fi
Maybe adding an array_diff_recursive function would be a good solution?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 22:01:27 2024 UTC