note 74956 added to function.array-intersect-key
| From: | RodByrnes at osu1 dot php dot net | Date: | Sun, 06 May 2007 04:10:42 +0000 |
| Subject: | note 74956 added to function.array-intersect-key | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126233@lists.php.net to get a copy of this message | ||
Here is a faster version than those shown below, with optimisation for the case when only two arrays
are passed. In my tests with a 10000 item first array and a 5000 item second array (run 20 times)
this function ran in 1.89 seconds compared with 2.66 for the version posted by dak. For a three
array case, same as above but with the third array containing 3333 values, the timing is 3.25 for
this version compared with 3.7 for dak's version.
<?php
if (!function_exists('array_intersect_key'))
{
function array_intersect_key($isec, $keys)
{
$argc = func_num_args();
if ($argc > 2)
{
for ($i = 1; !empty($isec) && $i < $argc; $i++)
{
$arr = func_get_arg($i);
foreach (array_keys($isec) as $key)
{
if (!isset($arr[$key]))
{
unset($isec[$key]);
}
}
}
return $isec;
}
else
{
$res = array();
foreach (array_keys($isec) as $key)
{
if (isset($keys[$key]))
{
$res[$key] = $isec[$key];
}
}
return $res;
}
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 138.130.135.133
----
Manual Page -- http://www.php.net/manual/en/function.array-intersect-key.php
Edit -- https://master.php.net/note/edit/74956
Del: integrated -- https://master.php.net/note/delete/74956/integrated
Del: useless -- https://master.php.net/note/delete/74956/useless
Del: bad code -- https://master.php.net/note/delete/74956/bad+code
Del: spam -- https://master.php.net/note/delete/74956/spam
Del: non-english -- https://master.php.net/note/delete/74956/non-english
Del: in docs -- https://master.php.net/note/delete/74956/in+docs
Del: other reasons-- https://master.php.net/note/delete/74956
Reject -- https://master.php.net/note/reject/74956
Search -- https://master.php.net/manage/user-notes.php