nabokoval / php-dot-access
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: ^4.0|^5.0|^6.0
This package is not auto-updated.
Last update: 2024-09-30 05:54:00 UTC
README
Dot 以轻量级和快速的方式,通过点符号方便地访问数据数组。
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 nabokoval/php-dot-notation
用法
创建一个新的 Dot 对象
$dot = new \Nabokoval\Dot;
// With existing array
$dot = new \Nabokoval\Dot($array);
你还可以使用辅助函数来创建对象
$dot = dot();
// With existing array
$dot = dot($array);
方法
Dot 具有以下方法
- add()
- all()
- clear()
- count()
- delete()
- flatten()
- get()
- has()
- isEmpty()
- merge()
- mergeRecursive()
- mergeRecursiveDistinct()
- 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'
]);
flatten()
返回一个扁平化的数组,键由给定的字符(默认为".")分隔
$flatten = $dot->flatten();
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);
mergeRecursive()
递归合并给定的数组或另一个 Dot 对象
$dot->mergeRecursive($array);
// Equivalent vanilla PHP
array_merge_recursive($originalArray, $array);
与给定的键递归合并给定的数组或另一个 Dot 对象
$dot->mergeRecursive('user', $array);
// Equivalent vanilla PHP
array_merge_recursive($originalArray['user'], $array);
mergeRecursiveDistinct()
递归合并给定的数组或另一个 Dot 对象。重复键将覆盖原始数组中的值(与 mergeRecursiveDistinct() 不同,其中重复键被转换成包含多个值的数组)
$dot->mergeRecursiveDistinct($array);
与给定的键递归合并给定的数组或另一个 Dot 对象。重复键将覆盖原始数组中的值。
$dot->mergeRecursiveDistinct('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->setArray($array);
setReference()
将所有项目替换为给定的数组作为引用,并且所有未来的 Dot 变更都将直接应用于原始数组
$dot->setReference($array);
toJson()
以 JSON 格式返回给定键的值
echo $dot->toJson('user');
以 JSON 格式返回存储的所有项目
echo $dot->toJson();