selvinortiz / dot
PHP 简化的数组点表示法
v1.1.0
2016-07-07 04:14 UTC
Requires
- phpunit/phpunit: ^4.8
This package is auto-updated.
Last update: 2024-09-08 06:52:26 UTC
README
描述
Dot 是一个小的库,实现了由 Selvin Ortiz 编写的 PHP 的数组 点表示法
需求
- PHP 7.1+
- Composer 和 selvinortiz/dot
安装
composer require selvinortiz/dot
测试
sh spec.sh
使用
在正确安装 Dot 后,只需 自动加载 它,使用 它,并在它上面调用方法即可:)
$input = [ 'name' => [ 'first' => 'Brad', 'last' => 'Bell', ], 'spouse' => [ 'name' => [ 'first' => 'Brandon', 'last' => 'Kelly' ], 'mood' => 'Happy', 'age' => '75', ], 'mood' => 'Angry', 'age' => 25, ]; Dot::has($input, 'spouse'); // true Dot::has($input, 'mistress.relationship'); // false Dot::get($input, 'spouse.name.last'); // 'Kelly' Dot::get($input, 'spouse'); /* [ 'name' => [ 'first' => 'Brandon', 'last' => 'Kelly' ], 'mood' => 'Happy', 'age' => '75' ] */ Dot::set($input, 'spouse.name.last', 'Bell'); /* $input will be mutated with a changed value [ 'name' => [ 'first' => 'Brad', 'last' => 'Bell', ], 'spouse' => [ 'name' => [ 'first' => 'Brandon', 'last' => 'Bell' ], 'mood' => 'Happy', 'age' => '75', ], 'mood' => 'Angry', 'age' => 25, ]; */ Dot::delete($input, 'spouse.mood'); /* $input will be mutated with a key/value deleted [ 'name' => [ 'first' => 'Brad', 'last' => 'Bell', ], 'spouse' => [ 'name' => [ 'first' => 'Brandon', 'last' => 'Bell' ] 'age' => '75', ], 'mood' => 'Angry', 'age' => 25, ]; */
API
Dot 有一个非常小的 API,并希望保持它小而精炼。
Dot::has($arr, $key)
返回
$arr
是否包含$key
。换句话说,$key
是否存在于$arr
中
Dot::get($arr, $key, $default = null)
通过
$key
在$arr
中返回找到的值或提供的$default
Dot::set(array &$arr, $key, $value)
通过添加提供的
$key
和$value
来修改$arr
Dot::delete(array &$arr, $key)
如果找到,通过删除
$key
和其关联的值来修改$arr
贡献
Dot 希望对 第一次贡献者 友好。只需按照以下步骤操作,如果在过程中有任何问题,请随时联系。
- 创建分支!
- 创建您的
bugfix
或feature
分支 - 提交并推送您的更改
- 提交 pull request
许可证
Dot 是开源软件,根据 MIT 许可证 许可