kamyshev/array_find

This package is abandoned and no longer maintained. The author suggests using the kamyshev/array_actions package instead.

A PHP function that find element in an array by a callback.

v1.0 2018-02-15 12:36 UTC

README

Packagist Build Status GitHub license

[DEPRECATED] This is no longer supported, please consider using array_actions instead.

array_find

A PHP function that find element in an array by a callback.

Installation

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

$ composer require kamyshev/array_find

If you do not want to use Composer, you can just require the src/array_find.php file.

Usage

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',
    ],
];

$grouped = 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.