firstred / php-dot-notation
PHP 数组点符号访问
2.0.2
2019-03-06 02:33 UTC
Requires
- php: >=5.3.3
Requires (Dev)
- phpunit/phpunit: ^4.0|^5.0|^6.0
- squizlabs/php_codesniffer: ^3.0
README
Dot 以轻量级和快速的方式,通过点符号提供对数据数组的便捷访问。灵感来源于 Laravel Collection。
Dot 实现了 PHP 的 ArrayAccess 接口,Dot 对象也可以像常规数组一样使用,并附加点符号。
示例
使用 Dot,你可以改变这种常规数组语法
$array['info']['home']['address'] = 'Kings Square'; echo $array['info']['home']['address']; // Kings Square
到这种(Dot 对象)
$dot->set('info.home.address', 'Kings Square'); echo $dot->get('info.home.address');
甚至这种(ArrayAccess)
$dot['info.home.address'] = 'Kings Square'; echo $dot['info.home.address'];
安装
使用 Composer 安装最新版本
$ composer require adbario/php-dot-notation
用法
创建一个新的 Dot 对象
$dot = new \Adbar\Dot; // With existing array $dot = new \Adbar\Dot($array);
您还可以使用辅助函数来创建对象
$dot = dot(); // With existing array $dot = dot($array);
方法
Dot 具有以下方法
- add()
- all()
- clear()
- count()
- delete()
- get()
- has()
- isEmpty()
- merge()
- pull()
- push()
- set()
- setArray()
- setReference()
- toJson()
add()
如果键不存在,则设置给定的键/值对
$dot->add('user.name', 'John'); // Equivalent vanilla PHP if (!isset($array['user']['name'])) { $array['user']['name'] = 'John'; }
多个键/值对
$dot->add([ 'user.name' => 'John', 'page.title' => 'Home' ]);
all()
返回存储的所有项作为数组
$values = $dot->all();
clear()
删除给定键的内容(设置空数组)
$dot->clear('user.settings'); // Equivalent vanilla PHP $array['user']['settings'] = [];
多个键
$dot->clear(['user.settings', 'app.config']);
存储的所有项
$dot->clear(); // Equivalent vanilla PHP $array = [];
count()
返回给定键中项的数量
$dot->count('user.siblings');
Dot 对象的根项
$dot->count(); // Or use coun() function as Dot implements Countable count($dot);
delete()
删除给定的键
$dot->delete('user.name'); // ArrayAccess unset($dot['user.name']); // Equivalent vanilla PHP unset($array['user']['name']);
多个键
$dot->delete([ 'user.name', 'page.title' ]);
get()
返回给定键的值
echo $dot->get('user.name'); // ArrayAccess echo $dot['user.name']; // Equivalent vanilla PHP < 7.0 echo isset($array['user']['name']) ? $array['user']['name'] : null; // Equivalent vanilla PHP >= 7.0 echo $array['user']['name'] ?? null;
如果给定的键不存在,则返回给定的默认值
echo $dot->get('user.name', 'some default value');
has()
检查给定的键是否存在(返回布尔值 true 或 false)
$dot->has('user.name'); // ArrayAccess isset($dot['user.name']);
多个键
$dot->has([ 'user.name', 'page.title' ]);
isEmpty()
检查给定的键是否为空(返回布尔值 true 或 false)
$dot->isEmpty('user.name'); // ArrayAccess empty($dot['user.name']); // Equivalent vanilla PHP empty($array['user']['name']);
多个键
$dot->isEmpty([ 'user.name', 'page.title' ]);
检查整个 Dot 对象
$dot->isEmpty();
merge()
合并给定的数组或另一个 Dot 对象
$dot->merge($array); // Equivalent vanilla PHP array_merge($originalArray, $array);
合并给定的数组或另一个 Dot 对象,并指定键
$dot->merge('user', $array); // Equivalent vanilla PHP array_merge($originalArray['user'], $array);
pull()
返回给定键的值并删除该键
echo $dot->pull('user.name'); // Equivalent vanilla PHP < 7.0 echo isset($array['user']['name']) ? $array['user']['name'] : null; unset($array['user']['name']); // Equivalent vanilla PHP >= 7.0 echo $array['user']['name'] ?? null; unset($array['user']['name']);
如果给定的键不存在,则返回给定的默认值
echo $dot->pull('user.name', 'some default value');
返回存储的所有项作为数组,并清除 Dot 对象
$items = $dot->pull();
push()
将给定值推送到给定键数组的末尾
$dot->push('users', 'John'); // Equivalent vanilla PHP $array['users'][] = 'John';
将给定值推送到数组的末尾
$dot->push('John'); // Equivalent vanilla PHP $array[] = 'John';
set()
设置给定的键/值对
$dot->set('user.name', 'John'); // ArrayAccess $dot['user.name'] = 'John'; // Equivalent vanilla PHP $array['user']['name'] = 'John';
多个键/值对
$dot->set([ 'user.name' => 'John', 'page.title' => 'Home' ]);
setArray()
用给定的数组替换 Dot 对象中的所有项
$dot->setArray($array);
setReference()
用给定的数组(作为引用)替换 Dot 对象中的所有项,并且 Dot 对象的所有未来更改将直接应用于原始数组
$dot->setReference($array);
toJson()
以 JSON 格式返回给定键的值
echo $dot->toJson('user');
以 JSON 格式返回存储的所有项
echo $dot->toJson();