Bug->Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
From: requinix@php.net Date: Tue, 10 Nov 2020 01:57:36 +0000 Subject: Bug->Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function References: 1 Groups: php.doc.bugs Request: Send a blank email to doc-bugs+get-18106@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80346&edit=1 ID: 80346 Updated by: requinix@php.net Reported by: gilperon at gmail dot com -Summary: Functionarray_intersect_ukeynot working (really!) +Summary: Functionarray_intersect_ukeyrequires consistent comparison function Status: Open -Type: Bug +Type: Documentation Problem -Package: *General Issues +Package: Arrays related Operating System: ALL PHP Version: 7.4.12 Block user comment: N Private report: N New Comment: Indeed, this is user error: as with every other array sorting function (and the intersect/diff functions *do* perform sorting - for efficiency), the callback needs to be consistent in its comparisons. It must return less than/equal to/greater than, and doing something else will likely result in "garbage in, garbage out" behavior. This matter is going to be common to all sorting, intersection, diff, etc. functions. If the docs are amended then I suggest some ©pasta; for each affected page. PS: Returning $key2 - $key1 is technically backwards, but the final sort order doesn't really matter since this function only cares about equality between elements. Previous Comments: ------------------------------------------------------------------------ [2020-11-10 01:32:41] danack@php.net Changing the code: echo "DIFFERENT\n"; return 1; to: echo "DIFFERENT\n"; return $key2 - $key1; Makes your code work. It looks like this code may be sorting the arrays first, and sorting algorithms need to be stable: https://www.geeksforgeeks.org/stability-in-sorting-algorithms/ That's an educated guess. If it is that, it's a docs problem as stability probably should be mentioned. ------------------------------------------------------------------------ [2020-11-10 01:21:38] gilperon at gmail dot com Description: ------------ Run the test script. It has 2 arrays in such a way that only the key4appears on both. Usingarray_intersect_ukeydoes not return4, instead it returnsarray(0) {}. Usingarray_intersect_keyworks perfectly. I also realized there is a bug in the implementation of this function, because if you run the test script it will show you this: |4|6|DIFFERENT |8|6|DIFFERENT |4|8|DIFFERENT |3|4|DIFFERENT |5|4|DIFFERENT |3|5|DIFFERENT |6|4|DIFFERENT |6|5|DIFFERENT |6|3|DIFFERENT It shouldnt be comparing 8 with 6 because these keys only appear on the 1st array and these keys shouldnt be compared at all. You can clearly see that this function, instead of comparing each key of the first array with each key of the second array, it's comparing the keys from the first array WITH ITSELF and then comparing the keys of the second array ITSELF (line |3|5|) and only then comparing the keys of both arrays together (but for some reason it misses the |4|4|). I know that in my simple use case I should be usingarray_intersect_keybut I really needarray_intersect_ukeyto work properly because I have a much more complex use case for it. Test script: --------------- <?php $array1 = array(4 => true, 6 => true, 8 => true); $array2 = array(3 => true, 4 => true, 5 => true); var_dump(array_intersect_ukey( $array1, $array2, function ($key1, $key2) { echo "|" . $key1 . "|" . $key2 . "|"; if ($key1 === $key2) { echo "EQUAL\n"; return 0; } else{ echo "DIFFERENT\n"; return 1; } } )); ?> Expected result: ---------------- It should only show the element 4, which is present in both arrays. Actual result: -------------- Nothing is returned from the intersection. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=80346&edit=1
Thread (8 messages)
- Unknown Message
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Unknown Message
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Unknown Message
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Unknown Message
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Unknown Message
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Unknown Message
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Unknown Message
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Unknown Message
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Opn]: Function `array_intersect_ukey`requires consistent comparison function
- Doc #80346 [Com]: Function `array_intersect_ukey`requires consistent comparison function
| « previous | php.doc.bugs (#18106) | next » |
|---|