kamyshev/array_actions

此包已被废弃,不再维护。未建议替代包。

数组简单操作集合

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

This package is auto-updated.

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


README

数组简单操作集合

安装

要获取 array_actions 的最新版本,只需使用 Composer 需求项目

$ composer require kamyshev/array_actions

用法

操作列表

array_find

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

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

示例输出

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

如果未找到元素,array_find 返回 null。

array_head

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

$head = array_head($records);

示例输出

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

如果数组为空,array_head 返回 null。