Skip to content

Latest commit

 

History

History
68 lines (53 loc) · 1.27 KB

File metadata and controls

68 lines (53 loc) · 1.27 KB

get_each

Description

Returns an array with the values of the key of each item in a collection. An empty array is returned if no item contains the key.

Parameters

key
Key to search in the collection.
coll
Collection where search the expected key.

Examples

Returns an array with emails from each element of an array:

<?php

use function Lambdish\Phunctional\get_each;

$users = [
    'user1' => [
        'name'  => 'Mike',
        'email' => 'mike@example.com'
    ],
    'user2' => [
        'name'  => 'John',
        'email' => 'john@example.com'
    ],
    'user3' => [
        'name'  => 'James',
        'phone' => '555-555555'
    ]
];

return get_each('email', $users);

// => ['mike@example.com', 'john@example.com']

Returns an empty array because the expected key does not exist in any item from the collection:

<?php

use function Lambdish\Phunctional\get_each;

$users = [
    'user1' => [
        'name'  => 'Mike',
        'email' => 'mike@example.com'
    ],
    'user2' => [
        'name'  => 'John',
        'email' => 'john@example.com'
    ],
    'user3' => [
        'name'  => 'James',
        'phone' => '555-555555'
    ]
];

return get_each('surname', $users);

// => []