php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33422 array_diff_assoc behaving different in PHP5 and PHP 4.3
Submitted: 2005-06-21 16:56 UTC Modified: 2005-06-21 17:09 UTC
From: fjortiz at comunet dot es Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.0.4 OS: Win2K Server
Private report: No CVE-ID: None
 [2005-06-21 16:56 UTC] fjortiz at comunet dot es
Description:
------------
array_diff_assoc in PHP5 seems to ignore items whose values  are objects. This should be irrelevant as this function takes only keys as reference for comparison. See code.


Reproduce code:
---------------
class Foo	{
	var $dato;	
	function Foo($dato)	{
		$this->dato = $dato;	
	}
}

$array1 = array ("a" => new Foo(5), "b" => new Foo(2), "c" => "azul", "rojo");
$array2 = array ("a" => new Foo(5), "amarillo", "rojo");
$result = array_diff_assoc ($array1, $array2);

print_r($result);

Expected result:
----------------
PHP 4.3.8:

Output:
Array
(
    [b] => foo Object
        (
            [dato] => 2
        )

    [c] => azul
    [0] => rojo
)

Correct.

The same code with PHP 5.0.4:
Output:
Array
(
    [a] => Foo Object
        (
            [dato] => 5
        )

    [b] => Foo Object
        (
            [dato] => 2
        )

    [c] => azul
    [0] => rojo
)

which is not the same, and not correct in PHP5: [a] item should not be there.



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-06-21 17:00 UTC] tony2001@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 behaviour, because objects in PHP5 are compared as descriptors, i.e. even two identical objects of the same class are different.
 [2005-06-21 17:07 UTC] fjortiz at comunet dot es
ok, me again, I saw the 
"array_diff_assoc() requires that both the key and the value pairs match."

so I guess PHP5 now considers both new Foo(5) objects as different (PHP4.3 does bitwise comparison) so they are included in result array. 

Still I find annoying that this breaks compatibility in some apps (it certainly does in mine...). 

Any suggestions? Isn't there anything like "operator==" in C++, so I can use array_diff_assoc safely (same key&value)? 

"PHP does not support operator overloading"

arrrg
 [2005-06-21 17:09 UTC] fjortiz at comunet dot es
ok bogus then, but doesn't solve my problem. Thanks :-)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 16:01:31 2024 UTC