Edit report at http://bugs.php.net/bug.php?id=52867&edit=1

 ID:                 52867
 Comment by:         uramihsayibok at gmail dot com
 Reported by:        jason at linuxbox dot com
 Summary:            array_intersect_ukey does not check all keys
 Status:             Open
 Type:               Bug
 Package:            Arrays related
 Operating System:   Linux
 PHP Version:        5.3.3
 Block user comment: N

 New Comment:

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;

  }

}


Previous Comments:
------------------------------------------------------------------------
[2010-09-17 04:18:19] 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

(

)


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=52867&edit=1

Reply via email to