midnite81/loopy

为可迭代对象添加功能

1.0.1 2022-05-03 09:39 UTC

This package is auto-updated.

Last update: 2024-08-30 01:06:11 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License Build Coverage Status

这是一个PHP包,它添加了一些命名空间数组函数。其中一些函数可以通过array_*原生访问,但也有一些函数不是原生可用的。

安装

此包适用于PHP7.4及以上版本。使用composer进行安装

composer require midnite81\loopy

可用函数

定义

each

each(iterable $items, Closure $callback): void

此函数遍历每个项目。如果闭包的结果返回false,则将在返回false的位置中断迭代。

示例

use function Midnite81\Loopy\each;

$colours = [
    'blue',
    'red',
    'green',
    'yellow'
];

each($colours, function($colour, $key) {
    echo $colour . " is at index " . $key . "\n";
});

结果

blue is at index 0
red is at index 1
green is at index 2
yellow is at index 3

all

all(iterable $items, Closure $callback): bool

此函数检查闭包的结果($callback)是否为真,适用于所有传递的可迭代对象($items)的迭代。

示例

use function Midnite81\Loopy\all;

$employees = [
    "id395" => ["name" => 'bob', "age" => 42, "dept" => 2],
    "id492" => ["name" => 'dave', "age" => 34, "dept" => 2],
    "id059" => ["name" => 'susan', "age" => 23, "dept" => 2],
];

$allBobs = all($employees, fn($employee) => $employee['name'] === 'bob');
// please note the key is also passed to the closure; therefore if the key is necessary
// to your function you could for example do the following 
// $allBobs = all($employees, fn($employee, $key) => $employee['name'] === 'bob' && $key != 'id000');

结果

$allBobs = false;
// thankfully, not everyone in the department is called bob

some

some(iterable $items, Closure $callback): bool

此函数检查闭包的结果($callback)是否为真,适用于传递的可迭代对象($items)的一个或多个迭代。

示例

use function Midnite81\Loopy\some;

$employees = [
    "id395" => ["name" => 'bob', "age" => 42, "dept" => 2],
    "id492" => ["name" => 'dave', "age" => 34, "dept" => 2],
    "id059" => ["name" => 'susan', "age" => 23, "dept" => 2],
];

$allBobs = some($employees, fn($employee) => $employee['name'] === 'bob');

// please note the key is also passed to the closure; therefore if the key is necessary
// to your function you could for example do the following 
// $allBobs = some($employees, fn($employee, $key) => $employee['name'] === 'bob' && $key != 'id000');

结果

$allBobs = true;
// one or more people in the department are called bob

map

map(iterable $items, Closure $callback): array

示例

use function Midnite81\Loopy\map;

$employees = [
    "id395" => ["name" => 'bob', "age" => 42, "dept" => 2],
    "id492" => ["name" => 'dave', "age" => 34, "dept" => 2],
    "id059" => ["name" => 'susan', "age" => 23, "dept" => 2],
];

$allBobs = map($employees, fn($employee, $key) => $employee['name']');

结果

$allBobs = [
    'bob',
    'dave',
    'susan',
]

reduce

reduce(iterable $items, Closure $callback, string|int|float $initial = ""): string|int|float

此函数将数组的值减少到单个字符串、整数或浮点数。

示例

use function Midnite81\Loopy\reduce;

$moneyReceived = [
    20.00,
    3.92,
    3.01,
    27.00
];

$totalMoneyReceived = reduce($moneyReceived, fn($current, $value, $key) => (float)$current + $value);
// $current is the current value of the reducer

结果

$totalMoneyReceived = 53.93;

filter

filter(iterable $items, Closure $callback, bool $preserveKey = false): array

此函数通过只包括闭包($callback)中的true来过滤传递的可迭代对象($items)。默认情况下,不保留键,但您可以将其设置为true,如果您希望保留键。

示例

use function Midnite81\Loopy\filter;

$users = [
            ["name" => 'dave'],
            ["name" => 'susan'],
            ["name" => 'ingrid'],
            ["name" => 'patricia'],
            ["name" => 'sally'],
        ];

$usersWhoseNamesDontStartWithS = filter($users, fn($user) => !str_starts_with($user['name'], "s"));

结果

$usersWhoseNamesDontStartWithS = [
    'dave',
    'ingrid',
    'patricia'
]

times

times(iterable $items, Closure $callback, int $times): bool

此函数检查回调实例($callback)在可迭代对象($items)中应出现的指定次数。

示例

use function Midnite81\Loopy\times;

$peopleOnTheBus = [
    'andy',
    'bob',
    'sally',
    'wendy',
    'bob'
];

$areThereTwoBobsOnTheBus = times($peopleOnTheBus, fn($people) => $people === 'bob', 2);

结果

$areThereTwoBobsOnTheBus = true;

once

once(iterable $items, Closure $callback): bool

once与times相同,但它将确保闭包的结果在传递的可迭代对象中只出现一次;

示例

use function Midnite81\Loopy\once;

$peopleOnTheBus = [
    'andy',
    'bob',
    'sally',
    'wendy'
];

$isThereJustOneAndyOnTheBus = once($peopleOnTheBus, fn($people) => $people === 'andy');

结果

$isThereJustOneAndyOnTheBus = true;