Reputation:
I have array like this:
$user = [
'id' => 1,
'fname' => 'name1',
'lname' => 'lname',
'age' => 20
];
I want to get values by given keys. Is there function already.
$userData = array....($user, ['fname', 'lname']); // get only fname and lname from user
I dont want to to for loops or similar.
Thanks
Upvotes: 1
Views: 118
Reputation: 782683
You can use array_intersect_key
, after flipping the second array to an associative array.
$userData = array_intersect_key($user, array_flip(['fname', 'lname']));
Upvotes: 4