kamyshev / array_find
A PHP function that find element in an array by a callback.
Installs: 2 408
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: ^6.5
This package is not auto-updated.
Last update: 2022-02-01 13:12:17 UTC
README
[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
.