|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-09-17 04:18 UTC] jason at linuxbox dot com
Description:
------------
The array_intersect_ukey() function appears to have an invalid algorithm for
iterating over array keys to be passed to the user function. A simple test case
that implements the same behavior as array_intersect_key() illustrates the bug.
Test script:
---------------
<?php
function func($key1, $key2) {
if ($key1 == $key2) {
return 0;
}
return 1;
}
$all = array('cat' => 'brown',
'dog' => 'yellow',
'mouse' => 'grey',
'horse' => 'white'
);
$keep = array('dog' => 1, 'horse' => 1);
// Output of the following should be identical
print_r(array_intersect_key($all, $keep));
print_r(array_intersect_ukey($all, $keep, 'func'));
?>
Expected result:
----------------
Array
(
[dog] => yellow
[horse] => white
)
Array
(
[dog] => yellow
[horse] => white
)
Actual result:
--------------
Array
(
[dog] => yellow
[horse] => white
)
Array
(
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 04 15:00:01 2025 UTC |
Not a bug. The manual page for array_intersect_ukey says >It must return an integer less than, equal to, or greater than zero if the >first key is considered to be respectively less than, equal to, or greater than >the second. Your callback does not do that and by violating the postcondition you get undefined behavior. function func($key1, $key2) { // equivalent to 'return strcmp($key1, $key2);' if ($key1 == $key2) { return 0; } else if ($key1 < $key2) { return -1; } else /* if ($key1 > $key2) */ { return 1; } }