Bug #62115 [Opn->Nab]: Issue with method array_diff_assoc
Edit report at https://bugs.php.net/bug.php?id=62115&edit=1
ID: 62115
Updated by: maarten@php.net
Reported by: patttern at gmail dot com
Summary: Issue with method array_diff_assoc
-Status: Open
+Status: Not a bug
Type: Bug
Package: Arrays related
Operating System: All
PHP Version: 5.4.3
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2012-05-23 13:38:56] 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-23 05:29:56] 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";
?>
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62115&edit=1
Thread (6 messages)