php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63833 diff doesn't compare properly inside arrays
Submitted: 2012-12-22 12:38 UTC Modified: 2012-12-22 14:03 UTC
From: francois dot dambrine at isen-lille dot fr Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.5.0alpha2 OS: Windows
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: francois dot dambrine at isen-lille dot fr
New email:
PHP Version: OS:

 

 [2012-12-22 12:38 UTC] francois dot dambrine at isen-lille dot fr
Description:
------------
This bug was introduced to me by a stackoverflow post 
http://stackoverflow.com/questions/13815287/union-two-associative-array-
php/13815578#13815578 I tested the behaviour with php 5.5 Alpha 2 and the "bug" is 
still there.

As of the documentation, it is said that array_diff uses an "internal function" to 
compare two elements.

As equality is the only thing that matters for array_diff I thought this internal 
function was something that "==" behind.

But even if two arrays are equal (same members) array_diff does not take it in 
account.

I mark it as a bug but it can only be a documentation problem.

Test script:
---------------
<?php 
$A = Array(
    array(
            'lable' =>"label0",
            'id_poste'=>1,
            'id_part'=>11
    ),
    array(
            'lable' =>"label1",
            'id_poste'=>2,
            'id_part'=>12
            ),
    array(
            'lable' =>"label2",
            'id_poste'=>3,
            'id_part'=>13
    ),
    array(
            'lable' =>"label3",
            'id_poste'=>4,
            'id_part'=>14
            )
);

$B = Array(
    array(
            'lable' =>"label0",
            'id_poste'=>1,
            'id_part'=>11
    ),
    array(
            'lable' =>"label1_X",
            'id_poste'=>2,
            'id_part'=>12
            ),
    array(
            'lable' =>"label2",
            'id_poste'=>3,
            'id_part'=>13
    ),
    array(
            'lable' =>"label3_X",
            'id_poste'=>4,
            'id_part'=>14
            )
);

var_dump(array_diff_assoc($B,$A));

Expected result:
----------------
Array(2){
    [0]=>array(3){
            ["lable"] =>"label1_X",
            ["id_poste"]=>2,
            ["id_part"]=>12
            },
    [1]=>array(3){
            ["lable"] =>"label3_X",
            ["id_poste"]=>4,
            ["id_part"]=>14
            }
}


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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-22 13:01 UTC] rasmus@php.net
The documentation clearly states, "This function only checks one dimension of a 
n-dimensional array. Of course you can check deeper dimensions by using 
array_diff($array1[0], $array2[0]);."

Your example is using 2-dimensional arrays.
 [2012-12-22 13:01 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2012-12-22 13:30 UTC] francois dot dambrine at isen-lille dot fr
I know it is multidimensional array. My problem is that if I want to compute the 
difference between those thwo multidimentional arrays. 
For now I can use array_udiff to get only the subarray with "lable"=>"Label1X" and 
Label2X, I have to use array_udiff even if when I test $A[0] == $B[0] I get true.
So I was wondering if it was a bug or just a lack of precision in the 
documentation.
If it is a documentation problem, I can go to edit.php.net
 [2012-12-22 13:31 UTC] francois dot dambrine at isen-lille dot fr
problem in the title
 [2012-12-22 13:31 UTC] francois dot dambrine at isen-lille dot fr
-Summary: udiff doesn't compare properly inside arrays +Summary: diff doesn't compare properly inside arrays
 [2012-12-22 14:03 UTC] rasmus@php.net
I still don't understand what you are asking about. array_diff being 1-
dimensional means it doesn't look inside nested arrays at all. If you have a 
nested array, it treats it as if it was "Array" because it casts all elements in 
that dimension to its scalar equivalent. For example, these two are identical as 
far as array_diff() is concerned:

$a = [ 1, 2, [3, 4] ];
$b = [ 1, 2, "Array" ];
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 13 11:01:33 2025 UTC