chipslays / array
Library for array manipulate.
Installs: 1 128
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires (Dev)
- phpunit/phpunit: ^9.5
README
Simple library for array manipulate.
Supported dot-notation and asterisks rules.
Installation
$ composer require chipslays/array
Documentation
Notice: Class
Collection
implementsCountable
andArrayAccess
.
Methods
get(array $array, $keys [, $default = null, string $separator = '.'])
Get value from array using by dot notation key.
Has helper arr_get()
.
use Chipslays\Arr\Arr; $array = [ 'user' => [ 'name' => 'chipslays' ], ]; $name = Arr::get($array, 'user.name'); // chipslays $email = Arr::get($array, 'user.email', 'default@email.com'); // default@email.com
$array = [ 'foo' => [ 'bar' => ['baz' => 1], 'bam' => ['baz' => 2], 'boo' => ['baz' => 3], ], ]; $results = arr_get($array, 'foo.*.baz'); // Array // ( // [0] => 1 // [1] => 2 // [2] => 3 // )
$array = [ 'foo' => [ 'bar' => ['baz' => 1], ], ]; $results = arr_get($array, 'foo.*.baz'); // 1
$array = [ 'foo' => [ 'bar' => ['baz' => 1], 'bam' => ['baz' => 2], 'boo' => ['baz' => 3], ], ]; $results = arr_get($array, 'foo.*'); // Array // ( // [0] => Array // ( // [baz] => 1 // ) // [1] => Array // ( // [baz] => 2 // ) // [2] => Array // ( // [baz] => 3 // ) // )
$array = [ 'foo' => [ 'bar' => ['baz' => 1], ], ]; $results = arr_get($array, 'foo.*'); // Array // ( // [baz] => 1 // )
set(array &$array, string $keys, $value = null [, string $separator = '.']) : void
Set/overwrite value in array using by dot notation key.
Has helper arr_set()
.
use Chipslays\Arr\Arr; $array = [ 'user' => [ 'name' => 'chipslays' ], ]; Arr::set($array, 'user.name', 'john doe'); Arr::set($array, 'user.email', 'john.doe@email.com'); Array ( [user] => Array ( [name] => john doe [email] => john.doe@email.com ) )
has(array $array, $keys [, string $separator = '.']): bool
Check exists value in array using by dot notation key.
Has helper arr_has()
.
use Chipslays\Arr\Arr; $array = [ 'user' => [ 'name' => 'chipslays', 'string' => '', 'null' => null, 'false' => false, ], ]; Arr::has($array, 'user.name'); // true Arr::has($array, 'user.string'); // true Arr::has($array, 'user.null'); // true Arr::has($array, 'user.false'); // true Arr::has($array, 'user.empty_value'); // false
where(array $array, callable $callback)
Filter the array using the given callback.
Has helper arr_where()
.
use Chipslays\Arr\Arr; $array = [ 'user' => [ 'name' => 'chipslays', 'string' => '', 'null' => null, 'false' => false, ], ]; // todo
👀 See also
chipslays/collection
- Simple library for manipulating array or object as collection.
License
MIT