kamyshev/array_actions

This package is abandoned and no longer maintained. No replacement package was suggested.

Set of simple actions with arrays

v1.1.1 2018-10-25 11:34 UTC

This package is auto-updated.

Last update: 2020-01-25 14:38:36 UTC


README

Set of simple actions with arrays

Installation

To get the latest version of array_actions, simply require the project using Composer:

$ composer require kamyshev/array_actions

Usage

Actions list:

array_find

To use array_find, simply pass an array and callback:

$records = [
    [
        'state'  => 'IN',
        'city'   => 'Indianapolis',
        'object' => 'School bus',
    ],
    [
        'state'  => 'IN',
        'city'   => 'Indianapolis',
        'object' => 'Manhole',
    ],
    [
        'state'  => 'IN',
        'city'   => 'Plainfield',
        'object' => 'Basketball',
    ],
    [
        'state'  => 'CA',
        'city'   => 'San Diego',
        'object' => 'Light bulb',
    ],
    [
        'state'  => 'CA',
        'city'   => 'Mountain View',
        'object' => 'Space pen',
    ],
];

$found = array_find($records, function ($state) {
    return $state['city'] === 'San Diego';
});

Example output:

Array
(
    [state] => CA
    [city] => San Diego
    [object] => Light bulb
)

If the element was not found, array_find returns null.

array_head

To use array_find, simply pass an array and callback:

$records = [
    [
        'state'  => 'IN',
        'city'   => 'Indianapolis',
        'object' => 'School bus',
    ],
    [
        'state'  => 'IN',
        'city'   => 'Indianapolis',
        'object' => 'Manhole',
    ],
    [
        'state'  => 'IN',
        'city'   => 'Plainfield',
        'object' => 'Basketball',
    ],
    [
        'state'  => 'CA',
        'city'   => 'San Diego',
        'object' => 'Light bulb',
    ],
    [
        'state'  => 'CA',
        'city'   => 'Mountain View',
        'object' => 'Space pen',
    ],
];

$head = array_head($records);

Example output:

Array
(
    [state] => IN
    [city] => Indianapolis
    [object] => School bus
)

If the array is empty, array_head returns null.