note 94467 deleted from function.array-intersect-key by danbrown

From: Date: Wed, 18 Nov 2009 14:23:53 +0000
Subject: note 94467 deleted from function.array-intersect-key by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-163027@lists.php.net to get a copy of this message
Note Submitter: Anonymous ---- 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' => '')); ?>

« previous php.notes (#163027) next »