BigToach.com

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($arrayarray_fill_keys($keys1));
}
?>

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
)
*/
?>

Added on Dec 12th at 7 pm by Scott - 0 Comments


0 Responses to array_select_key

Name:

Site or Mail:

Comment:

To try to reduce the ammount of spam, please enter the word GOOSE in the following form

Security Code: