PHP Snippets
array_select_key
Information
(PHP Version >= 3)Functions used in this snippet: array_intersect_key, array_fill_keys.
Description
array array_select_key( array array, array keys )
Returns any values from the array $array which have keys that are in the array $keys
Snippet
<?php
function array_select_key($array, $keys) {
return array_intersect_key($array, array_fill_keys($keys, 1));
}
?>
Example
array_select_key Can be used in the following way:
<?php
$post = array(
'name' => 'Scott',
'email' => 'fakeaddress@scott.com',
'phone' => '5555551212',
'submit' => 'Add Contact'
);
$wanted = array('name', 'email', 'phone');
$insert = array_select_key($post, $wanted);
print_r($insert);
/*
Array
(
[name] => Scott
[email] => fakeaddress@scott.com
[phone] => 5555551212
)
*/
?>
0 Responses to array_select_key