Bug #53724 [NEW]: array_diff like methods compares false as identity and true as equal

From: Date: Wed, 12 Jan 2011 13:16:40 +0000
Subject: Bug #53724 [NEW]: array_diff like methods compares false as identity and true as equal
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-157849@lists.php.net to get a copy of this message
From:             
Operating system: Irrelevant
PHP version:      5.3.5
Package:          Arrays related
Bug Type:         Bug
Bug description:array_diff like methods compares false as identity and true as equal

Description:
------------
array_diff functions compares boolean false as identity (operator "===")
and boolean true as equal (operator "==") so behaves inconsistently. It
should compare all values in same way and in my opinion it should compare
as equals ("==") or have argument (or another mechanism) how to set whether
compare by equal or identity.

As example I use array_diff_assoc but I think it will probably behave
simmilary with another array realted functions (array_diff%,
array_intersect%). In expected result I suppose comparing as equal
(operator '==').

In example you can see, that array_diff_assoc behaves in such way that 0 or
'0' or '' is not same as false, but 1 or '1' is same as true. So false
is
compared like '===' and true is compared like '=='. Because 0 is same as
'0' and 1 is same as '1' I suppose, that this problem is only for boolean
false value.


Test script:
---------------
$arr1 = array('a' => 0,     'b' => 1,    'c' =>
'0',   'd' => '1',  'e' =>
'',    'f' => false, 'g' => true, 'h' => 0,  
'i' => 1);
$arr2 = array('a' => false, 'b' => true, 'c' => false,
'd' => true, 'e' =>
false, 'f' => false, 'g' => true, 'h' => '0',
'i' => '1');

var_dump(array_diff_assoc($arr1, $arr2));

Expected result:
----------------
array(0) {
}


Actual result:
--------------
array(2) {
  ["a"]=>
  int(0)
  ["c"]=>
  string(1) "0"
}


-- 
Edit bug report at http://bugs.php.net/bug.php?id=53724&edit=1
-- 
Try a snapshot (PHP 5.2):            http://bugs.php.net/fix.php?id=53724&r=trysnapshot52
Try a snapshot (PHP 5.3):            http://bugs.php.net/fix.php?id=53724&r=trysnapshot53
Try a snapshot (trunk):              http://bugs.php.net/fix.php?id=53724&r=trysnapshottrunk
Fixed in SVN:                        http://bugs.php.net/fix.php?id=53724&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=53724&r=needdocs
Fixed in release:                    http://bugs.php.net/fix.php?id=53724&r=alreadyfixed
Need backtrace:                      http://bugs.php.net/fix.php?id=53724&r=needtrace
Need Reproduce Script:               http://bugs.php.net/fix.php?id=53724&r=needscript
Try newer version:                   http://bugs.php.net/fix.php?id=53724&r=oldversion
Not developer issue:                 http://bugs.php.net/fix.php?id=53724&r=support
Expected behavior:                   http://bugs.php.net/fix.php?id=53724&r=notwrong
Not enough info:                     http://bugs.php.net/fix.php?id=53724&r=notenoughinfo
Submitted twice:                     http://bugs.php.net/fix.php?id=53724&r=submittedtwice
register_globals:                    http://bugs.php.net/fix.php?id=53724&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=53724&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=53724&r=dst
IIS Stability:                       http://bugs.php.net/fix.php?id=53724&r=isapi
Install GNU Sed:                     http://bugs.php.net/fix.php?id=53724&r=gnused
Floating point limitations:          http://bugs.php.net/fix.php?id=53724&r=float
No Zend Extensions:                  http://bugs.php.net/fix.php?id=53724&r=nozend
MySQL Configuration Error:           http://bugs.php.net/fix.php?id=53724&r=mysqlcfg



Thread (5 messages)

« previous php.bugs (#157849) next »