note 94467 added to function.array-intersect-key

From: Date: Fri, 06 Nov 2009 18:24:31 +0000
Subject: note 94467 added to function.array-intersect-key
Groups: php.notes 
Request: Send a blank email to php-notes+get-162462@lists.php.net to get a copy of this message
Here's a variation on array_intersect_key() that intersects the first (master) array's keys with the values of other arrays. It can also be used to intersect an array with an arbitrary list of keys (à la compact(), but using a source array instead of current variables): <?php function array_intersect_keylist(array $source) { $final = $source; // Loop through all arguments passed foreach(func_get_args() as $cx=> $keylist) if ($cx) { if (!is_array($keylist)) $keylist = Array((string)$keylist); // Intersect $final = array_intersect_key($final, array_fill_keys($keylist, '')); } return $final; } // Example usage $test = Array('foo' => 'something', 'bar' => 'whatever'); // Returns Array('foo' => 'something') array_intersect_keylist($test, Array('foo', 'baz') ); // Also returns Array('foo' => 'something') array_intersect_keylist($test, 'foo', 'baz'); // Equivalent to... array_intersect_key($test, Array('foo' => '', 'baz' => '')); ?> ---- Server IP: 64.71.164.2 Probable Submitter: 67.237.238.105 ---- Manual Page -- http://www.php.net/manual/en/function.array-intersect-key.php Edit -- https://master.php.net/note/edit/94467 Del: integrated -- https://master.php.net/note/delete/94467/integrated Del: useless -- https://master.php.net/note/delete/94467/useless Del: bad code -- https://master.php.net/note/delete/94467/bad+code Del: spam -- https://master.php.net/note/delete/94467/spam Del: non-english -- https://master.php.net/note/delete/94467/non-english Del: in docs -- https://master.php.net/note/delete/94467/in+docs Del: other reasons-- https://master.php.net/note/delete/94467 Reject -- https://master.php.net/note/reject/94467 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#162462) next »