theodorejb/array-utils
Composer 安装命令:
composer require theodorejb/array-utils
包简介
Useful functions for working with arrays
README 文档
README
ArrayUtils is a collection of useful PHP array functions.
Install via Composer
composer require theodorejb/array-utils
Methods
containsAll
Returns true if all the needles are in the haystack.
use theodorejb\ArrayUtils\ArrayUtils; $haystack = [1, 2, 3, 5, 8, 13]; $needles = [2, 13, 5]; ArrayUtils::containsAll($needles, $haystack); // true ArrayUtils::containsAll($haystack, $needles); // false
containsSame
Returns true if both arrays contain the same values (regardless of order).
use theodorejb\ArrayUtils\ArrayUtils; $set1 = [1, 3, 5, 7]; $set2 = [3, 7, 5, 1]; ArrayUtils::containsSame($set1, $set2); // true
groupRows
Splits the iterable of arrays into groups when the value of one or more keys changes. The iterable must be sorted by the array keys used to group results.
use theodorejb\ArrayUtils\ArrayUtils; $peoplePets = [ ['lName' => 'Jordan', 'fName' => 'Jack', 'pet' => 'Scruffy'], ['lName' => 'Jordan', 'fName' => 'Jack', 'pet' => 'Spot'], ['lName' => 'Jordan', 'fName' => 'Jill', 'pet' => 'Paws'], ['lName' => 'Greene', 'fName' => 'Amy', 'pet' => 'Blackie'], ['lName' => 'Greene', 'fName' => 'Amy', 'pet' => 'Whiskers'], ['lName' => 'Greene', 'fName' => 'Amy', 'pet' => 'Paws'], ['lName' => 'Smith', 'fName' => 'Amy', 'pet' => 'Tiger'], ]; $grouped = []; foreach (ArrayUtils::groupRows($peoplePets, 'fName') as $group) { $grouped[] = $group; } $expected = [ [$peoplePets[0], $peoplePets[1]], [$peoplePets[2]], [$peoplePets[3], $peoplePets[4], $peoplePets[5], $peoplePets[6]], ]; var_dump($grouped === $expected); // bool(true) ////// Additional arguments can be passed to `groupRows` to group by more than one column: $grouped = []; foreach (ArrayUtils::groupRows($peoplePets, 'lName', 'fName') as $group) { $grouped[] = $group; } $expected = [ [$peoplePets[0], $peoplePets[1]], [$peoplePets[2]], [$peoplePets[3], $peoplePets[4], $peoplePets[5]], [$peoplePets[6]], ]; var_dump($grouped === $expected); // bool(true)
requireStrKey
Returns the specified array key value if it is a string. Throws an exception otherwise.
use theodorejb\ArrayUtils\ArrayUtils; $data = ['k' => 'val', 'i' => 1]; ArrayUtils::requireStrKey($data, 'k'); // val ArrayUtils::requireStrKey($data, 'x'); // throws OutOfBoundsException ArrayUtils::requireStrKey($data, 'i'); // throws UnexpectedValueException
getOptionalStrKey
Returns the specified array key value if it is a string, or null if the array key doesn't exist.
Throws an exception if the key exists but the value is not a string.
use theodorejb\ArrayUtils\ArrayUtils; $data = ['k' => 'val', 'i' => 1]; ArrayUtils::getOptionalStrKey($data, 'k'); // val ArrayUtils::getOptionalStrKey($data, 'x'); // null ArrayUtils::getOptionalStrKey($data, 'i'); // throws UnexpectedValueException
requireNumericKey
Returns the specified array key value as a float if it is an integer or float. Throws an exception otherwise.
use theodorejb\ArrayUtils\ArrayUtils; $data = ['i' => 1, 'f' => 0.5, 'k' => 'val']; ArrayUtils::requireNumericKey($data, 'i'); // 1.0 ArrayUtils::requireNumericKey($data, 'f'); // 0.5 ArrayUtils::requireNumericKey($data, 'x'); // throws OutOfBoundsException ArrayUtils::requireNumericKey($data, 'k'); // throws UnexpectedValueException
getOptionalNumericKey
Returns the specified array key value as a float if it is an integer or float, or null if the array key doesn't exist.
Throws an exception if the key exists but the value is not an integer or float.
use theodorejb\ArrayUtils\ArrayUtils; $data = ['i' => 2, 'f' => 0.5, 'k' => 'val']; ArrayUtils::getOptionalNumericKey($data, 'i'); // 2.0 ArrayUtils::getOptionalNumericKey($data, 'f'); // 0.5 ArrayUtils::getOptionalNumericKey($data, 'x'); // null ArrayUtils::getOptionalNumericKey($data, 'k'); // throws UnexpectedValueException
requireIntKey
Returns the specified array key value if it is an integer. Throws an exception otherwise.
use theodorejb\ArrayUtils\ArrayUtils; $data = ['k' => 'val', 'i' => 1]; ArrayUtils::requireIntKey($data, 'i'); // 1 ArrayUtils::requireIntKey($data, 'x'); // throws OutOfBoundsException ArrayUtils::requireIntKey($data, 'k'); // throws UnexpectedValueException
getOptionalIntKey
Returns the specified array key value if it is an integer, or null if the array key doesn't exist.
Throws an exception if the key exists but the value is not an integer.
use theodorejb\ArrayUtils\ArrayUtils; $data = ['k' => 'val', 'i' => 2]; ArrayUtils::getOptionalIntKey($data, 'i'); // 2 ArrayUtils::getOptionalIntKey($data, 'x'); // null ArrayUtils::getOptionalIntKey($data, 'k'); // throws UnexpectedValueException
requireBoolKey
Returns the specified array key value if it is a boolean. Throws an exception otherwise.
use theodorejb\ArrayUtils\ArrayUtils; $data = ['k' => 'val', 'b' => true]; ArrayUtils::requireBoolKey($data, 'b'); // true ArrayUtils::requireBoolKey($data, 'x'); // throws OutOfBoundsException ArrayUtils::requireBoolKey($data, 'k'); // throws UnexpectedValueException
getOptionalBoolKey
Returns the specified array key value if it is a boolean, or null if the array key doesn't exist.
Throws an exception if the key exists but the value is not a boolean.
use theodorejb\ArrayUtils\ArrayUtils; $data = ['k' => 'val', 'b' => false]; ArrayUtils::getOptionalBoolKey($data, 'b'); // false ArrayUtils::getOptionalBoolKey($data, 'x'); // null ArrayUtils::getOptionalBoolKey($data, 'k'); // throws UnexpectedValueException
Author
Theodore Brown
https://theodorejb.me
License
MIT
theodorejb/array-utils 适用场景与选型建议
theodorejb/array-utils 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.47k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2016 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 theodorejb/array-utils 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 theodorejb/array-utils 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 6.47k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-01-13