note 47857 added to function.array-intersect-key

From: Date: Thu, 02 Dec 2004 21:26:01 +0000
Subject: note 47857 added to function.array-intersect-key
Groups: php.notes 
Request: Send a blank email to php-notes+get-81298@lists.php.net to get a copy of this message
anarcat's array_intersect_key() implementation checks for key presence using isset() rather than array_key_exists(). Hence: <?php $a = array ('foo' => 'bar'); $b = array ('foo' => null); var_dump(array_intersect_key($a, $b)); ?> returns an empty array (ie, no matching keys), which is probably not right. Also, the documented function signature takes two parameters, minimum, while anarcat's accepted one. Consider this implementation instead: <?php function array_intersect_key() { $numArgs = func_num_args(); if (2 <= $numArgs) { $arrays =& func_get_args(); for ($idx = 0; $idx < $numArgs; $idx++) { if (! is_array($arrays[$idx])) { trigger_error('Parameter ' . ($idx+1) . ' is not an array', E_USER_ERROR); return false; } } foreach ($arrays[0] as $key => $val) { for ($idx = 1; $idx < $numArgs; $idx++) { if (! array_key_exists($key, $arrays[$idx])) { unset($arrays[0][$key]); } } } return $arrays[0]; } trigger_error('Not enough parameters; two arrays expected', E_USER_ERROR); return false; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.array-intersect-key.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+47857 Delete -- http://master.php.net/manage/user-notes.php?action=delete+47857&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47857&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47857&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47857&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47857&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47857&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+47857&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#81298) next »