php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61118 array_intersect_assoc() with duplicate arrays for values gives of a notice
Submitted: 2012-02-17 07:36 UTC Modified: 2012-02-17 17:24 UTC
From: letharion at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.4.0RC7 OS: Gentoo Linux
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: letharion at gmail dot com
New email:
PHP Version: OS:

 

 [2012-02-17 07:36 UTC] letharion at gmail dot com
Description:
------------
Gentoo package version: dev-lang/php-5.4.0_rc7-r1

Configure: ./configure --prefix=/usr --build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu --mandir=/usr/share/man --infodir=/usr/share/info --datadir=/usr/share --sysconfdir=/etc --localstatedir=/var/lib --prefix=/usr/lib64/php5.4 --mandir=/usr/lib64/php5.4/man --infodir=/usr/lib64/php5.4/info --libdir=/usr/lib64/php5.4/lib --with-libdir=lib64 --without-pear --disable-maintainer-zts --disable-bcmath --with-bz2 --disable-calendar --enable-ctype --with-curl --with-curlwrappers --enable-dom --without-enchant --enable-exif --enable-fileinfo --enable-filter --disable-ftp --with-gettext --without-gmp --enable-hash --without-mhash --with-iconv --disable-intl --enable-ipv6 --enable-json --without-kerberos --enable-libxml --enable-mbstring --with-mcrypt --without-mssql --with-onig=/usr --with-openssl --with-openssl-dir=/usr --disable-pcntl --enable-phar --enable-pdo --without-pgsql --enable-posix --with-pspell --without-recode --enable-simplexml --disable-shmop --without-snmp --disable-soap --disable-sockets --without-sqlite3 --without-sybase-ct --disable-sysvmsg --disable-sysvsem --disable-sysvshm --without-tidy --enable-tokenizer --disable-wddx --enable-xml --disable-xmlreader --disable-xmlwriter --without-xmlrpc --without-xsl --disable-zip --with-zlib --disable-debug --enable-dba --without-cdb --with-db4 --disable-flatfile --with-gdbm --disable-inifile --without-qdbm --with-freetype-dir=/usr --with-t1lib=/usr --disable-gd-jis-conv --with-jpeg-dir=/usr --with-png-dir=/usr --without-xpm-dir --with-gd --with-ldap --without-ldap-sasl --with-mysql=mysqlnd --with-mysql-sock=/var/run/mysqld/mysqld.sock --with-mysqli=mysqlnd --without-pdo-dblib --with-pdo-mysql=mysqlnd --without-pdo-pgsql --without-pdo-sqlite --without-pdo-odbc --with-readline --without-libedit --without-mm --with-pcre-regex=/usr --with-pcre-dir=/usr --with-config-file-path=/etc/php/cli-php5.4 --with-config-file-scan-dir=/etc/php/cli-php5.4/ext-active --disable-embed --enable-cli --disable-cgi --disable-fpm --without-apxs2


Test script:
---------------
<?php

$foo = array('baz' => Array  (0 => 'X' ));
$bar = array('baz' => Array  (1 => 'Y' ));

print_r(array_intersect_assoc($foo, $bar));


Expected result:
----------------
Array
(
    [baz] => Array
        (
            [0] => X
        )

)

Actual result:
--------------
PHP Notice:  Array to string conversion in array_intersect_assoc_short.php on line 6

Notice: Array to string conversion in array_intersect_assoc_short.php on line 6
PHP Notice:  Array to string conversion in array_intersect_assoc_short.php on line 6

Notice: Array to string conversion in array_intersect_assoc_short.php on line 6

Array
(
    [baz] => Array
        (
            [0] => X
        )

)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-17 08:37 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2012-02-17 08:37 UTC] nikic@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This is expected behavior: The notice indicates incorrect usage of the function. array_intersect does not compare the arrays but casts them to strings thus making both "Array" and issuing a notice.

The behavior always has been like this, but the notice was added in 5.4 :)
 [2012-02-17 08:52 UTC] letharion at gmail dot com
Thank you for pointing this out to me. Sorry about the "no-bug".
 [2012-02-17 17:24 UTC] rasmus@php.net
Your example in this bug is also a good illustration of why the notice is 
needed. This function does't support nested arrays. It does a single-level 
comparison and as such it actually gave you an unintuitive result. The 
intersection is supposed to give you the values that appear in both arrays and 
have the same keys. So if you compared: 

  ['baz' => 1] and ['baz' => 2]

there are no intersecting values. However, if you nest those:

  ['baz' => [1] ] and ['baz' => [2] ]

Now you suddenly have a result. This is because it doesn't look at the contents 
of the nested arrays, it just converts them to strings and compares them. This 
would give you the same result:

  ['baz' => [1] ] and ['baz' => 'Array']

which is probably not what you had in mind. That's why you are getting a notice 
here.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 13:01:30 2024 UTC