php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33854 array_diff depending on element sequence
Submitted: 2005-07-25 21:23 UTC Modified: 2005-07-26 12:54 UTC
From: bob dot siefkes at packardbell dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.0.4 OS: XP SP2
Private report: No CVE-ID: None
 [2005-07-25 21:23 UTC] bob dot siefkes at packardbell dot com
Description:
------------
Behavor of array_diff is dependent on the sequence of the elements in the array.

See $array2 where I changed the sequence of "a"=>"c" with "a"=>"d" and it results in different output. 

Be noted that the key of both elements is the same, still I would not expect array_diff to take this into account.

Bob

Reproduce code:
---------------
// source 1
$array1 = array("a" => "c", "b" => "b" );
$array2 = array("a" => "c", "a" => "d" );
$result = array_diff($array1, $array2);

// source 2
$array1 = array("a" => "c", "b" => "b" );
$array2 = array("a" => "d", "a" => "c" );
$result = array_diff($array1, $array2);


Expected result:
----------------
// result1:
Array
(
    [a] => c
    [b] => b
)


// result2
Array
(
    [b] => b
)

Actual result:
--------------
// for both I would expect

Array
(
    [b] => b
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-25 21:26 UTC] bob dot siefkes at packardbell dot com
I did confuse the two result blocks...
Expected result: <--> Actual result:
 [2005-07-25 22:32 UTC] iliaa@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

if you want keys & position to be considered use the array_diff_assoc() function.
 [2005-07-26 08:38 UTC] bob dot siefkes at packardbell dot com
I'm aware of the assoc variant. Still I'm not convinced that array_diff behaves regular.

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

The value "c" exist in array1 and in array2. The result does also contain value "c". I would not expect to see "c" in the result.
 [2005-07-26 12:54 UTC] mgf@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

Your $array2 isn't valid -- you can't have two array elements with the same key.  From your given results, it looks like PHP is only keeping the second "a"-> element in each case.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 07:01:32 2024 UTC