php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #69059 array_udiff logical bug
Submitted: 2015-02-16 01:03 UTC Modified: 2015-03-01 04:22 UTC
From: geoleu11 at gmail dot com Assigned:
Status: No Feedback Package: *General Issues
PHP Version: Irrelevant OS: Ubuntu
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2015-02-16 01:03 UTC] geoleu11 at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.array-udiff
---

Since the information that can be selected for providing php version information, here is some info about the build : PHP Version 5.5.9-1ubuntu4.5 .

Also regarding the problem/bug source : array function, more specifically : array_udiff .

The output of the below script is : 

array (size=1)
  0 => string 'red' (length=3)  

Why does array_udiff return a common element from those arrays? Shouldn't it return the elements are present in the first array and not in the other arrays?

At least if you try internal array_diff, it does return the proper value from those 2 arrays below .

Test script:
---------------
$a1 = array('red');
$a2 = array('red', 'blue');

$diff = array_udiff($a1, $a2,
        function($a, $b) {
            if($a === $b) {
                return 0;
            }
            else {
                return -1;
            }
        });

var_dump($diff);

Expected result:
----------------
The expected result should be an empty array.

array (size=0)
  empty  

Actual result:
--------------
array (size=1)
  0 => string 'red' (length=3)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-16 03:38 UTC] laruence@php.net
-Status: Open +Status: Feedback
 [2015-02-16 03:38 UTC] laruence@php.net
compare function should return value greater than 0 if $a > $b, less than 0 if $b < $a.

thus, your codes should be:

<?php
$a1 = array('red');
$a2 = array('red', 'blue');

$diff = array_udiff($a1, $a2,
    function($a, $b) {
        if($a === $b) {
            return 0;
        }
        else {
            return $a > $b? 1 : -1;
        }
    });

var_dump($diff);


anyway, this bug doesn't exists in master because new sort algo impelemented.

thanks
 [2015-02-16 03:38 UTC] laruence@php.net
s ,bug,issue,
 [2015-02-16 15:33 UTC] geoleu11 at gmail dot com
Yes, I saw that if you return unsigned integer the sort/looping is different. That is why I said there is a logical bug. The result should be the same no matter if you use unsigned/signed negative integer. It should be a simple comparison : are those 2 values equal or unequal. This is a good way to properly compare array of objects. I ended up using a php made version of array_udiff.
Is 'red' > || < 'blue'? It's not about counting the bits, it's simply about checking for equality.

Having a logic/scenario for 3 type of returns seems prone to bugs (at least in my case was). That is why array_udiff should treat negative int and unsigned int the same. So basically one scenario for 0 and one for negative/unsigned int.

If there is a fix relating to this function in the current php dev version, a documentation update would be nice that explains how negative int is treated compared to a unsinged int for the callback function.

Thanks
 [2015-03-01 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 22:01:33 2024 UTC