note 74956 deleted from function.array-intersect-key by cmb

From: Date: Mon, 02 Nov 2020 21:44:42 +0000
Subject: note 74956 deleted from function.array-intersect-key by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-215770@lists.php.net to get a copy of this message
Note Submitter: Rod Byrnes ---- 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; } } } ?>

« previous php.notes (#215770) next »