PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » PHP » PHP_Compat » Bug #6293

array_intersect_key produces incorrect results with more than 2 arguments

Details

Submitted2005-12-19 23:20 UTC
Fromcourt at idstrom dot com
Assignedarpad
StatusClosed
PackagePHP_Compat
PHP Version5.0.5
OSMac OSX 10.4.3
Roadmaps1.6.0a1

Comments

[2005-12-19 23:20 UTC] court at idstrom dot com

Description:
------------
PHP_Compat version 1.5

Test script:
---------------
<?

require_once('PHP/Compat/Function/array_intersect_key.php');

print_r(array_intersect_key(
array('a'=>1, 'b'=>2, 'c'=>3, 'd'=>4),
array('a'=>0, 'c'=>0),
array('a'=>0, 'd'=>4)
));

?>

Here's a correct (and slightly faster) implementation that requires php 4 >= 4.0.4 (for call_user_func_array)

function array_intersect_key() {
$args = func_get_args();

$arg_keys = array_map('array_keys', $args);
$result_keys = call_user_func_array('array_intersect', $arg_keys);

$first_arg = $args[0];
$result = array();
foreach($result_keys as $key) {
$result[$key] = $first_arg[$key];
}
return $result;
}
}

Expected result:
----------------
Array
(
[a] => 1
)

Actual result:
--------------
Array
(
[a] => 1
[c] => 3
[d] => 4
)