php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42341 array_diff regression 5.1->5.2 on subobjects
Submitted: 2007-08-19 12:22 UTC Modified: 2007-08-20 14:26 UTC
From: steven dot mccoy at miru dot hk Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.4RC2 OS: Ubuntu
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: steven dot mccoy at miru dot hk
New email:
PHP Version: OS:

 

 [2007-08-19 12:22 UTC] steven dot mccoy at miru dot hk
Description:
------------
Upgrading from PHP 5.1.6-1 (Debian) to 5.2.1 (Ubuntu) a regression has appeared in array_diff().  Comparisons now fail when an array member is a stdClass object.

Also reproduced with 5.2.4RC1-dev.

Reproduce code:
---------------
<?php
$dogs = array();
$poodle = new stdClass;
$poodle->collar = 'red';
array_push($dogs, $poodle);
array_push($dogs, 'terrier');
array_push($dogs, 'dalmation');

$cat = array('persian' => array(1, 2, 3));

var_dump(array_diff($dogs, $cat));
?>


Expected result:
----------------
array(3) {
  [0]=>
  object(stdClass)#1 (1) {
    ["collar"]=>
    string(3) "red"
  }
  [1]=>
  string(7) "terrier"
  [2]=>
  string(9) "dalmation"
}


Actual result:
--------------
Catchable fatal error: Object of class stdClass could not be converted to string in /miru/home/steve-o/- on line 11

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-19 17:38 UTC] judas dot iscariote at gmail dot com
Use array_udiff with user supplied callback, the old behaviuor looks wrong to me.

array_diff does compare with (string) $elem1 === (string) $elem2 and as stdclass does not implement __toString() it emits a correct fatal error.
 [2007-08-19 19:59 UTC] jani@php.net
Expected behaviour -> bogus.
 [2007-08-20 14:26 UTC] steven dot mccoy at miru dot hk
Awesome, here's a possible compat function:

function compare_diff516($a, $b)
{
        if (is_object($a) || is_object($b))
                return $a == $b;
        return strcmp($a, $b);
}
function array_diff516($a, $b)
{
        return array_udiff($a, $b, 'compare_diff516');
}
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 01:01:35 2025 UTC