prowebcraft/dot
Composer 安装命令:
composer require prowebcraft/dot
包简介
PHP dot notation array access
关键字:
README 文档
README
Based on adbario/php-dot-notation package
Easy access to multidimensional arrays with dot notation. With dot notation, your code is cleaner and handling deeper arrays is super easy.
This class implements PHP's ArrayAccess class, so Dot object can also be used the same way as normal arrays with additional dot notation.
With Dot you can change this:
echo $data['info']['home']['address'];
to this:
echo $data->get('info.home.address');
or even this:
echo $data['info.home.address'];
Installation
Via composer:
composer require prowebcraft/dot
Or just copy the class file Dot.php and handle namespace yourself.
With Composer:
composer require prowebcraft/dot
Manual installation:
- Download the latest release
- Extract the files into your project
- require_once '/path/to/dot/src/Dot.php';
Usage
This array will be used as a reference on this guide:
$array = [ 'user' => [ 'firstname' => 'John', 'lastname' => 'Smith' ], 'info' => [ 'kids' => [ 0 => 'Laura', 1 => 'Chris', 2 => 'Little Johnny' ], 'home' => [ 'address' => 'Rocky Road 3' ] ] ];
Create a Dot object
To start with an empty array, just create a new Dot object:
$data = new \Prowebcraft\Dot;
If you have an array already available, inject it to the Dot object:
$data = new \Prowebcraft\Dot($array);
Set an array after creating the Dot object:
$data->setArray($array);
Set an array as a reference, and all changes will be made directly to the original array:
$data->setReference($array);
Set a value
Set i.e. a phone number in the 'home' array:
$data->set('info.home.tel', '09-123-456-789'); // Array style $data['info.home.tel'] = '09-123-456-789';
Set multiple values at once:
$data->set([ 'user.haircolor' => 'blue', 'info.home.address' => 'Private Lane 1' ]);
If the value already exists, Dot will override it with a new value.
Get a value
echo $data->get('info.home.address'); // Default value if the path doesn't exist echo $data->get('info.home.country', 'some default value'); // Array style echo $data['info.home.address'];
Get all the stored values:
$values = $data->all(); `` Get a value from a path and remove it: ```php $address = $data->pull('home.address');
Get all the stored values and remove them:
$values = $data->pull();
Add a value
$data->add('info.kids', 'Amy');
Multiple values at once:
$data->add('info.kids', [ 'Ben', 'Claire' ]);
Check if a value exists
if ($data->has('info.home.address')) { // Do something... } // Array style if (isset($data['info.home.address'])) { // Do something... }
Delete a value
$data->delete('info.home.address'); // Array style unset($data['info.home.address']);
Multiple values at once:
$data->delete([ 'user.lastname', 'info.home.address' ]);
Clear values
Delete all the values from a path:
$data->clear('info.home');
Clear multiple paths at once:
$data->clear([ 'user', 'info.home' ]);
Clear all data:
$data->clear();
Sort the values
You can sort the values of a given path or all the stored values.
Sort the values of a path:
$kids = $data->sort('info.kids'); // Sort recursively $info = $data->sort('info');
Sort all the values
$sorted = $data->sort(); // Sort recursively $sorted = $data->sort();
Magic methods
Magic methods can be used to handle single level data (without dot notation). These examples are not using the same data array as examples above.
Set a value:
$data->name = 'John';
Get a value:
echo $data->name;
Check if a value exists:
if (isset($data->name)) { // Do something... }
Delete a value:
unset($data->name);
License
prowebcraft/dot 适用场景与选型建议
prowebcraft/dot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29.82k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2017 年 07 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「ArrayAccess」 「dotnotation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 prowebcraft/dot 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 prowebcraft/dot 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 prowebcraft/dot 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP dot notation access to arrays
Class to store and access data in a case-insensitive fashion, while maintaining the integrity and functionality of a regular array.
A collection package that can be extended to implement things such as a Dependency Injection Container, RecordSet objects for housing database records, a bag of http cookies, or technically any collection of items that can be looped over and whose items can each be accessed using array-access syntax
Trait providing methods to implement array access using a container variable.
Bundle for simple access to (mainly) nosql databases for Symfony
Work with magic fields
统计信息
- 总下载量: 29.82k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 15
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-10