midnite81/loopy 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

midnite81/loopy

最新稳定版本:1.0.1

Composer 安装命令:

composer require midnite81/loopy

包简介

Adds functionality for iterables

README 文档

README

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

This is a PHP package which adds some namespaced array functions. Some of these functions can be natively accessed using array_* however there are few which aren't natively available.

Installation

This package is available for PHP7.4+. To install use composer

composer require midnite81\loopy

Available Functions

Definitions

each

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

This function loops over each item. If the result of the closure returns false, then it will break the iteration at the point it returns false.

Example

use function Midnite81\Loopy\each;

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

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

Result

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

This function checks to see if the result of the closure ($callback) is true for all iterations of the iterable passed ($items)

Example

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');

Result

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

some

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

This function checks to see if the result of the closure ($callback) is true for one or more iterations of the iterable passed ($items)

Example

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');

Result

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

map

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

Example

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

Result

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

reduce

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

This function reduces down the values of an array to a single string, integer or float.

Example

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

Result

$totalMoneyReceived = 53.93;

filter

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

This function filters down the iterable ($items) passed by only including what is true in the Closure ($callback). By default, the key is not preserved, but you can set it to true, if you wish to preserve the key.

Example

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"));

Result

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

times

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

This function checks to see that the instance of the call back ($callback) should only be found the specified number of times in the iterable ($items)

Example

use function Midnite81\Loopy\times;

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

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

Result

$areThereTwoBobsOnTheBus = true;

once

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

Once is exactly the same as times however it will ensure the result of the closure only appears once in the iterable passed;

Example

use function Midnite81\Loopy\once;

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

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

Result

$isThereJustOneAndyOnTheBus = true;

统计信息

  • 总下载量: 235
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固