Bug #73427 [Com]: Unintended behaviour of intersection functions

From: Date: Tue, 17 Jul 2018 18:10:12 +0000
Subject: Bug #73427 [Com]: Unintended behaviour of intersection functions
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-216368@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73427&edit=1 ID: 73427 Comment by: turabgarip at gmail dot com Reported by: turabgarip at gmail dot com Summary: Unintended behaviour of intersection functions Status: No Feedback Type: Bug Package: Arrays related Operating System: Windows PHP Version: 5.6.27 Block user comment: N Private report: N New Comment: To test the bug, you can write a callback to print the compared elements to use with array_uintersect() like; $arr1 = array('green', 'blue', 'cyan'); $arr2 = array('yellow', 'red', 'cyan'); $i = 1; function print_elements($e1, $e2) { global $i; echo $i . '. ' . $e1 . ' compared to ' . $e2 . '<br>'; $i++; if ($e1 == $e2) return 0; else return 1; } print_r(array_uintersect($arr1, $arr2, 'print_elements')); The result will be; 1. green compared to blue 2. cyan compared to blue 3. green compared to cyan 4. yellow compared to red 5. cyan compared to red 6. yellow compared to cyan 7. blue compared to red 8. blue compared to cyan 9. blue compared to yellow Array ( ) You see that print_elements() is called for 9 times and green compares to blue; which are the elements of the first array only. And there are also missing comparisons; as you can see cyan in $arr1 didn't compare to cyan in $arr2 and thus the intersection returned an empty array. If you increase the elements in arrays; you will see that print_element() will be called for interesting times; which is obviously not the combination calculation of the arrays. This applies to all versions as far as I can see. I tested with PHP 5.6.x and PHP 7.1 By the way I lost the password for changing the bug status; would you help on that? Previous Comments: ------------------------------------------------------------------------ [2018-06-24 04:25:28] 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. ------------------------------------------------------------------------ [2018-05-21 06:46:02] requinix@php.net I don't see the "compare from the first three colors" behavior. Where/how are you seeing that? And I assume this also applies to PHP 7.1 and 7.2? ------------------------------------------------------------------------ [2018-05-21 06:24:56] turabgarip at gmail dot com Wow, it's been years and no one actually took a glance on it? :) ------------------------------------------------------------------------ [2016-10-31 08:14:30] turabgarip at gmail dot com Description: ------------ Intersection functions intend to compute the intersection of two or more arrays; but they try to compare the elements of the individual array itself. This is contradictory to the logic of intersection. Thus it makes the intersection functions considerably slow. In the test script, there are two arrays. Intersection will start to compare from the first three colors like; "blue" to "green", "black" to "blue"; that are, the elements of the first array only; which are actually very unnecessary to compare. Because the actual intention is to compute the intersection of the two arrays; not the intersection of the elements of the array itself. However an array can contain only one identical element at once anyway, so it's useless and resource consuming to check the array's own elements against each other. Test script: --------------- $arr1 = array('green', 'blue', 'black'); $arr2 = array('yellow', 'red', 'cyan'); array_intersect($arr1, $arr2); Expected result: ---------------- Intersection function should compare the elements of the given arrays crosswise. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=73427&edit=1

« previous php.bugs (#216368) next »