kamyshev / array_find
v1.0
2018-02-15 12:36 UTC
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
[已弃用] 此功能不再受支持,请考虑使用 array_actions 代替。
array_find
一个用于通过回调函数在数组中查找元素的PHP函数。
安装
要获取array_find的最新版本,只需使用Composer引入项目
$ composer require kamyshev/array_find
如果您不想使用Composer,可以直接require "src/array_find.php"文件。
使用方法
要使用array_find,只需传递一个数组和回调函数
$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'; });
示例输出
Array
(
[state] => CA
[city] => San Diego
[object] => Light bulb
)
如果未找到元素,array_find返回null。