php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39811 array_diff "compare with 2" bug
Submitted: 2006-12-13 07:49 UTC Modified: 2006-12-13 11:32 UTC
From: wishm at bk dot ru Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.4.4 OS: win32
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: wishm at bk dot ru
New email:
PHP Version: OS:

 

 [2006-12-13 07:49 UTC] wishm at bk dot ru
Description:
------------
array_diff() function fails on comparing any array element with 2, when another pair of elements are both set to 2. Fails with that settings only.

Reproduce code:
---------------
<?php
$a = array("2","2");
$b = array("2","any_symbols_here");
// same thing on $b = array("any_symbols_here","2");
print_r(array_diff($a,$b));
?>

Expected result:
----------------
Array ( [1] => 2 ) 

// second elements are not equal so function should return that element from $a array

Actual result:
--------------
Array ( )

//empty array - function failed

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-12-13 10:06 UTC] tony2001@php.net
http://php.net/array_diff

array_diff() returns an array containing **all the values of array1 that are not present in any of the other arguments*.

In your case all values of $a are present in $b.
 [2006-12-13 11:10 UTC] wishm at bk dot ru
I think you dont understand the purpose of the array_diff() function. The element is considered present if and only if its' VALUE is equal to the same-named element's VALUE in the second array! Try array_diff(array("a"=>"1"),array("a"=>"2")). The element a = 1 does not present at second array so it will be returned. You didnt even tried to understand the essence of the bug! It is 100% PHP error when 2 is considered equal to ANY value under conditions I've described. Please fix that!
 [2006-12-13 11:27 UTC] tony2001@php.net
>I think you dont understand the purpose of the array_diff() function.

And I think you didn't read what I said and keep ignoring the docs.

>The element is considered present if and only if its'
>VALUE is equal to the same-named element's VALUE in the
>second array!

No. Read the documentation first.
http://php.net/array_diff
 [2006-12-13 11:29 UTC] tony2001@php.net
There is even Russian translation of this particular page:
http://ru.php.net/manual/ru/function.array-diff.php
 [2006-12-13 11:29 UTC] wishm at bk dot ru
ok the problem was just at this function's "feature" - multiple occurrences in first array are all treated the same way. I was wondered that applies to the associative arrays, too. So indexes are simply ignored. Screwed up a bit... array_diff_assoc fixes that :) Sorry for the bothering.
 [2006-12-13 11:32 UTC] tony2001@php.net
.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 17:01:35 2025 UTC