Bug #70763 [Com]: Bug in array_udiff()?

From: Date: Mon, 16 May 2016 19:24:45 +0000
Subject: Bug #70763 [Com]: Bug in array_udiff()?
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201140@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70763&edit=1 ID: 70763 Comment by: inefedor at gmail dot com Reported by: jonjohnson1 at yandex dot com Summary: Bug in array_udiff()? Status: Open Type: Bug Package: *General Issues Operating System: Windows PHP Version: 5.6.14 Block user comment: N Private report: N New Comment: Here's how array_udiff works under the hood: https://3v4l.org/P5JdW It produces wrong result for you because the comparison function is incorrect. It should return -1 (or any integer less than zero) if $left is less than $right, 1 (or any integer greater than zero) if $left is greater than $right, and 0 if they are equal. Previous Comments: ------------------------------------------------------------------------ [2016-05-16 18:15:02] panguzol at gmail dot com Sorry, I made mistake in last example. It should be ``` echo '#5 '; $r = array_udiff($a, $b, function($x, $y) { return (int)!($x === $y); }); var_dump($r); ``` Actual result: ``` #5 array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) } ``` ------------------------------------------------------------------------ [2016-05-16 18:05:33] panguzol at gmail dot com Test code ``` <?php $a = [1,2,3,4,5]; $b = [2,3,5]; echo '#1 '; $r = array_udiff($a, $b, function($x, $y) { return $x === $y ? 0 : -1; }); var_dump($r); echo '<br />'; echo '#2 '; $r = array_udiff($a, $b, function($x, $y) { return $x === $y ? 0 : 1; }); var_dump($r); echo '<br />'; echo '#3 '; $r = array_udiff($a, $b, function($x, $y) { return $x - $y; }); var_dump($r); echo '<br />'; echo '#4 '; $r = array_udiff($a, $b, function($x, $y) { return $y - $x; }); var_dump($r); echo '<br />'; echo '#5 '; $r = array_udiff($a, $b, function($x, $y) { return (int)!$x === $y; }); var_dump($r); ?> ``` Expected output: ``` #1 array(2) { [0]=> int(1) [3]=> int(4) } #2 array(2) { [0]=> int(1) [3]=> int(4) } #3 array(2) { [0]=> int(1) [3]=> int(4) } #4 array(2) { [0]=> int(1) [3]=> int(4) } #5 array(2) { [0]=> int(1) [3]=> int(4) } ``` Actual output ``` #1 array(2) { [0]=> int(1) [3]=> int(4) } #2 array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) } #3 array(2) { [0]=> int(1) [3]=> int(4) } #4 array(2) { [0]=> int(1) [3]=> int(4) } #5 array(0) { } ``` As we can see in cases #2 and #5 we got wrong result. PS: Why comparison function must return integer not boolean? It shouldn't make difference whether other element is bigger or smaller, just is it equal or not. ------------------------------------------------------------------------ [2015-10-21 21:16:04] jonjohnson1 at yandex dot com Description: ------------ function value_compare_func($a, $b){ if ($a === 'n_3') { return 0; } return 1; } $array1 = array("n_1", "n_2", "n_3", "n_4" ); $array2 = array("green"); $result = array_udiff($array1, $array2, "value_compare_func"); print_r($result); The expected output is: Array([0] => 'n_1', [1] => 'n_2' , [3] => 'n_4' ) But PHP outputs: Array([1] => 'n_2' , [3] => 'n_4' ) Where is n_1? Test script: --------------- $array1 = array("n_1", "n_2", "n_3", "n_4" ); $array2 = array("green"); $result = array_udiff($array1, $array2, "value_compare_func"); print_r($result); Expected result: ---------------- Array([0] => 'n_1', [1] => 'n_2' , [3] => 'n_4' ) Actual result: -------------- Array([1] => 'n_2' , [3] => 'n_4' ) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70763&edit=1

« previous php.bugs (#201140) next »