php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62115 Issue with method array_diff_assoc
Submitted: 2012-05-23 05:29 UTC Modified: 2012-05-25 04:54 UTC
From: patttern at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.4.3 OS: All
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: patttern at gmail dot com
New email:
PHP Version: OS:

 

 [2012-05-23 05:29 UTC] patttern at gmail dot com
Description:
------------
While executing method array_diff_assoc error appears "Notice: Array to string 
conversion".
The output of script:
array(2) { ["one"]=> array(2) { ["param1"]=> string(23) "First Parameter for 
One" ["param2"]=> string(24) "Second Parameter for One" } ["two"]=> array(2) { 
["param1"]=> string(23) "First Parameter for Two" ["param2"]=> string(24) 
"Second Parameter for Two" } }
string(3) "two"
array(2) { ["param1"]=> string(23) "First Parameter for Two" ["param2"]=> 
string(24) "Second Parameter for Two" }
array(1) { ["two"]=> array(2) { ["param1"]=> string(23) "First Parameter for 
Two" ["param2"]=> string(24) "Second Parameter for Two" } }
Notice: Array to string conversion in /usr/web/data/test_array_diff.php on line 
14 Notice: Array to string conversion in /usr/web/data/test_array_diff.php on 
line 14 array(1) { ["one"]=> array(2) { ["param1"]=> string(23) "First Parameter 
for One" ["param2"]=> string(24) "Second Parameter for One" } }

What is wrong?

Test script:
---------------
<?php
$source_packages = array(
	'one' => array ('param1' => 'First Parameter for One', 'param2' => 'Second Parameter for One'),
	'two' => array ('param1' => 'First Parameter for Two', 'param2' => 'Second Parameter for Two')
);
$packages_from = $source_packages;
var_dump($packages_from); echo "<br />\n";
$package_key = 'two';
var_dump($package_key); echo "<br />\n";
$package_value = $source_packages[$package_key];
var_dump($package_value); echo "<br />\n";
$packages_to = array($package_key => $package_value);
var_dump($packages_to); echo "<br />\n";
$result_packages = array_diff_assoc($packages_from, $packages_to);
var_dump($result_packages); echo "<br />\n";
?>


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-23 13:38 UTC] arjen at react dot com
http://nl3.php.net/array_diff_assoc

"Two values from key => value pairs are considered equal only if (string) $elem1 === (string) $elem2 . In other words a strict check 
takes place so the string representations must be the same."

and 

"Note: This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for 
example, array_diff_assoc($array1[0], $array2[0]);."

So values are casted to string for comparision.

The array-elements are casted to string, which results in "array". The following example returns no difference between the two arrays, 
while this is clearly not the case: http://3v4l.org/1LX4W#v540

Without notices, both arrays would look the same.  Notices are generated since 5.4.0, which is a good solution IMO.
 [2012-05-24 08:40 UTC] maarten@php.net
-Status: Open +Status: Not a bug
 [2012-05-24 08:40 UTC] maarten@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

See the answer by arjen at react dot com.

The array_diff* methods aren't recursive. So you probably won't want to use them for multidimensional arrays, and thus it's an improvement that >= 5.4.0 warns you.
 [2012-05-25 04:54 UTC] patttern at gmail dot com
Thank you for your answers!
For a test I run this script on different versions of PHP 

<?php
echo "PHP version: ".phpversion()."<br />\n";
$source_packages = array(
	'one' => array ('param1' => 'First Parameter for One', 'param2' => 
'Second Parameter for One'),
	'two' => array ('param1' => 'First Parameter for Two', 'param2' => 
'Second Parameter for Two')
);
$packages_from = $source_packages;
var_dump($packages_from); echo "<br />\n";
$package_key = 'two';
$package_value = $source_packages[$package_key];
$packages_to = array($package_key => $package_value);
var_dump($packages_to); echo "<br />\n";
$result_packages = array_diff_assoc($packages_from, $packages_to);
var_dump($result_packages); echo "<br />\n";
?>
===================
PHP version: 5.3.10
array(2) { ["one"]=> array(2) { ["param1"]=> string(23) "First Parameter for 
One" ["param2"]=> string(24) "Second Parameter for One" } ["two"]=> array(2) { 
["param1"]=> string(23) "First Parameter for Two" ["param2"]=> string(24) 
"Second Parameter for Two" } }
array(1) { ["two"]=> array(2) { ["param1"]=> string(23) "First Parameter for 
Two" ["param2"]=> string(24) "Second Parameter for Two" } }
array(1) { ["one"]=> array(2) { ["param1"]=> string(23) "First Parameter for 
One" ["param2"]=> string(24) "Second Parameter for One" } } 
===================
PHP version: 5.4.3
array(2) { ["one"]=> array(2) { ["param1"]=> string(23) "First Parameter for 
One" ["param2"]=> string(24) "Second Parameter for One" } ["two"]=> array(2) { 
["param1"]=> string(23) "First Parameter for Two" ["param2"]=> string(24) 
"Second Parameter for Two" } }
array(1) { ["two"]=> array(2) { ["param1"]=> string(23) "First Parameter for 
Two" ["param2"]=> string(24) "Second Parameter for Two" } }
Notice: Array to string conversion in /usr/web/data/test_array_diff.php on line 
13 Notice: Array to string conversion in /usr/web/data/test_array_diff.php on 
line 13 array(1) { ["one"]=> array(2) { ["param1"]=> string(23) "First Parameter 
for One" ["param2"]=> string(24) "Second Parameter for One" } }
===================

Since 5.4+ version the work  of array_diff*  method changed. How I should 
rewrite this code to avoid errors? At the moment error log of a average visited 
site is increasing on 50+Mb every day because of this notice.
 [2012-05-25 11:33 UTC] arjen at react dot com
See http://nl3.php.net/manual/en/function.array-diff-assoc.php#73972
 [2012-05-25 11:46 UTC] patttern at gmail dot com
Thank you for answer!
It's solved.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Mar 13 13:01:30 2025 UTC