array-path
Yet another array path implementation
Requirements
- PHP 7.1+
Usage
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\NotFoundExceptionexists()
echo (new \PeeHaa\ArrayPath\ArrayPath())->exists(['foo' => ['bar' => 'baz']], 'foo.bar'); // true
echo (new \PeeHaa\ArrayPath\ArrayPath())->exists(['foo' => ['bar' => 'baz']], 'foo.qux'); // falseset()
$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) {
}
}
*/
Note: remove() will not throw when the key does not exist in the array.
On this page
Languages
PHP100.0%
Latest Release
v1.0.0October 31, 2017MIT License
Created September 30, 2017
Updated April 16, 2020


