update page now
Laravel Live Japan

Voting

: five minus zero?
(Example: nine)

The Note You're Voting On

nathan dot fiscaletti at gmail dot com
7 years ago
If you want an associative version of this you can do the following:

function array_slice_assoc($array,$keys) {
    return array_intersect_key($array,array_flip($keys));
}

However, if you want an inverse associative version of this, just use array_diff_key instead of array_intersect_key. 

function array_slice_assoc_inverse($array,$keys) {
    return array_diff_key($array,array_flip($keys));
}

Example:

$arr = [
    'name' => 'Nathan',
    'age' => 20,
    'height' => 6
];

array_slice_assoc($arr, ['name','age']);

will return 

Array (
     'name' = 'Nathan',
     'age' = 20
)

Where as

array_slice_assoc_inverse($arr, ['name']);

will return 

Array (
    'age' = 20,
    'height' = 6
)

<< Back to user notes page

To Top