opxcore/array
Composer 安装命令:
composer require opxcore/array
包简介
The OpxCore array utils.
关键字:
README 文档
README
Installing
composer require opxcore/array
Usage
Available methods:
For dot notation:
OpxCore\Arr\Arr::dot($array)OpxCore\Arr\Arr::get($array, $key, $default)OpxCore\Arr\Arr::set($array, $key, $value)OpxCore\Arr\Arr::has($array, $keys)OpxCore\Arr\Arr::forget($array, $keys)OpxCore\Arr\Arr::pull($array, $key, $default)OpxCore\Arr\Arr::push($array, $key, $value)
Regular arrays only:
OpxCore\Arr\Arr::only($array, $keys)OpxCore\Arr\Arr::first($array, $callback, $default)OpxCore\Arr\Arr::last($array, $callback, $default)
All $default values are optional and could be callable that returns value. By default, it is always null.
Performance tips: All methods for manipulating with dot notation are optimised for performance on a dot notated keys. If you are care about performance much it is better to use regular array manipulations on non-dot notated arrays.
Making a dot notated array
$dotNotated = Arr::dot($array);
Makes a dot notated array from any flat or multidimensional array.
$array = ['level1' => ['level2' => 'value']]; $result = Arr::dot($array); // $result === ['level1.level2' => 'value']
Getting a value using dot notation
OpxCore\Arr\Arr::get($array, $key, $default = null)
Gets a value from the array using dot notation. If the given key is not existing in the array, this function will return default value.
use OpxCore\Arr\Arr; $array = ['level1' => ['level2' => 'value']]; $result = Arr::get($array, 'level1.level2'); // $result === 'value' $result = Arr::get($array, 'level1.level3'); // $result === null $result = Arr::get($array, 'level1.level3', false); // $result === false $result = Arr::get($array, 'level1.level3', function(){return -1;}); // $result === -1
Setting a value using dot notation
OpxCore\Arr\Arr::set($array, $key, $value): array
Sets the value into the array for the given key using dot notation. Function modifies given array directly, but also returns modified array for convenient usage.
use OpxCore\Arr\Arr; $array = ['level1' => ['level2' => 'value']]; $result = Arr::set($array, 'level1.level2_1', 'another value'); // $result === ['level1' => ['level2' => 'value', 'level2_1' => 'another value']] // $array === $result
Determining a key (or keys) existence in the array
OpxCore\Arr\Arr::has($array, $keys)
Determines if the given key (or several keys) are existing in the array.
use OpxCore\Arr\Arr; $array = ['level1' => ['level2' => 'value', 'level2_1' => 'another value']]; $result = Arr::has($array, 'level1.level2'); // $result === true $result = Arr::has($array, ['level1.level2', 'level1.level2_2']); // $result === false
Removing the values associated with the given keys
OpxCore\Arr\Arr::forget($array, $keys)
Removes the given key (or several keys given as an array) from the array. If any of keys are not existing in the array nothing happens. Function modifies given array directly, but also returns modified array for convenient usage.
use OpxCore\Arr\Arr; $array = ['level1' => ['level2' => 'value', 'level2_1' => 'another value']]; $result = Arr::forget($array, 'level1.level2'); // $result === ['level1' => ['level2_1' => 'another value']] // $array === $result
Pulling the value from the array
OpxCore\Arr\Arr::pull($array, $key, $default)
Actually, this is combination of Arr::get() and Arr::forget() methods. This method
fetches the value and then removes it from the array. If the key is not exists in the
array, default value will be returned.
use OpxCore\Arr\Arr; $array = ['level1' => ['level2' => 'value', 'level2_1' => 'another value']]; $result = Arr::pull($array, 'level1.level2'); // $result === 'value' // $array === ['level1' => ['level2_1' => 'another value']]
Pushing the value into the array
OpxCore\Arr\Arr::push($array, $key, $value)
Pushes the value into the given key in array. If given key is not existing, it will be created and the value will be set as item of unassociated array. If the key is existing, value associated with this key will be casted as array and value will be added into it. Function modifies given array directly, but also returns modified array for convenient usage.
use OpxCore\Arr\Arr; $array = ['level1' => ['level2' => 'value']]; $result = Arr::push($array, 'level1.level2', 'another value'); // $result === ['level1' => ['level2' => ['value', 'another value']]] // $array === $result
opxcore/array 适用场景与选型建议
opxcore/array 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 141 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「array」 「dot notation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 opxcore/array 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 opxcore/array 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 opxcore/array 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
Access array data quickly/easily using dot-notation and asterisk.
A library for simulating dice rolls written in PHP.
A GraphViz generator for PHP. PEAR Image_GraphViz rethought.
^B A dead-simple PHP library to add caret notation in order to safely show strings that contain ASCII control characters (unprintable characters)
Trait providing methods to set class properties with an array.
统计信息
- 总下载量: 141
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 3
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-13