php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73617 Array diff output is wrong.
Submitted: 2016-11-28 16:54 UTC Modified: 2016-11-29 11:33 UTC
From: hello at bweston dot me Assigned:
Status: Not a bug Package: Arrays related
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
 [2016-11-28 16:54 UTC] hello at bweston dot me
Description:
------------
So I have the following

```
<?php

var_dump(array_diff(
    [
        'q1' => true,
        'q2' => true,
        'q3' => false,
        'q4' => false,
    ],
    [
        'q1' => false,
        'q2' => false,
        'q3' => false,
        'q4' => false,
    ]
));
```

Which produces: (https://3v4l.org/WSs2s)

```
array(2) {
  ["q1"]=>
  bool(true)
  ["q2"]=>
  bool(true)
}
```

Which is fine however with just one change it isn't seen. The following produces an empty array yet there is a change. (https://3v4l.org/DlnBr)

```
<?php

var_dump(array_diff(
    [
        'q1' => true,
        'q2' => true,
        'q3' => false,
        'q4' => false,
    ],
    [
        'q1' => false,
        'q2' => true,
        'q3' => false,
        'q4' => false,
    ]
));
```


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-11-28 16:59 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-11-28 16:59 UTC] requinix@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

array_diff is not associative. Try array_diff_assoc instead.
 [2016-11-29 11:21 UTC] hello at bweston dot me
Whoops, sorry, I thought assoc would be checking the key names. But you're right it does work.

However still don't understand why the results wasn't correct in the first place as it is the values which have changed.
 [2016-11-29 11:33 UTC] requinix@php.net
>as it is the values which have changed
array_diff only looks at the values. Read the description for the function: "...returns the values in array1 that are not present in any of the other arrays".
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Dec 14 21:01:23 2019 UTC