|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-10-31 08:14 UTC] 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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 16 02:00:02 2025 UTC |
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?