php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52867 array_intersect_ukey does not check all keys
Submitted: 2010-09-17 04:18 UTC Modified: 2010-11-25 20:57 UTC
From: jason at linuxbox dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3.3 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jason at linuxbox dot com
New email:
PHP Version: OS:

 

 [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
(
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-13 04:17 UTC] uramihsayibok at gmail dot com
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;
  }
}
 [2010-11-25 20:57 UTC] iliaa@php.net
-Status: Open +Status: Bogus
 [2010-11-25 20:57 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 06 23:01:28 2025 UTC