peehaa / array-path
v1.0.0
2017-10-31 15:24 UTC
Requires
- php: ^7.1
Requires (Dev)
- phpunit/phpunit: ^6
This package is auto-updated.
Last update: 2024-09-07 07:04:46 UTC
README
另一个数组路径实现
要求
- PHP 7.1+
用法
get()
echo (new \PeeHaa\ArrayPath\ArrayPath())->get(['foo' => ['bar' => 'baz']], 'foo.bar'); // baz echo (new \PeeHaa\ArrayPath\ArrayPath())->get(['foo' => ['bar' => 'baz']], 'foo.qux'); // throws \PeeHaa\ArrayPath\NotFoundException
exists()
echo (new \PeeHaa\ArrayPath\ArrayPath())->exists(['foo' => ['bar' => 'baz']], 'foo.bar'); // true echo (new \PeeHaa\ArrayPath\ArrayPath())->exists(['foo' => ['bar' => 'baz']], 'foo.qux'); // false
set()
$array = []; (new \PeeHaa\ArrayPath\ArrayPath())->set($array, 'foo.bar', 'value'); var_dump($array);
/**
array(1) {
["foo"]=>
array(1) {
["bar"]=>
string(5) "value"
}
}
*/
remove()
$array = ['foo' => ['bar' => 'value']]; (new \PeeHaa\ArrayPath\ArrayPath())->remove($array, 'foo.bar'); var_dump($array);
/**
array(1) {
["foo"]=>
array(0) {
}
}
*/
注意:当键在数组中不存在时,remove()
不会抛出异常。