Oihana PHP

pick.php

Table of Contents

Functions

pick()  : array<string|int, mixed>
Keeps only the specified keys from an array (inverse of removeKeys).

Functions

pick()

Keeps only the specified keys from an array (inverse of removeKeys).

pick(array<string|int, mixed> $array, array<string|int, mixed> $keys) : array<string|int, mixed>

This function returns a new array containing only the entries whose keys are present in the given keys list.

Parameters
$array : array<string|int, mixed>

The target array to filter.

$keys : array<string|int, mixed>

The list of keys to keep.

Tags
example
$data = [
    'id'    => 42,
    'name'  => 'Alice',
    'email' => 'alice@example.com',
    'role'  => 'admin',
];

$result = pick( $data , [ 'id' , 'name' ] );

// [
//     'id'   => 42,
//     'name' => 'Alice',
// ]
author

Marc Alcaraz (ekameleon)

since
1.0.8
Return values
array<string|int, mixed>

The filtered array containing only the specified keys.


        
On this page

Search results