chipslays/array

Library for array manipulate.

1.1.2 2021-07-03 10:42 UTC

This package is auto-updated.

Last update: 2024-10-29 05:41:25 UTC


README

Tests Packagist Version

Simple library for array manipulate.

Supported dot-notation and asterisks rules.

Installation

$ composer require chipslays/array

Documentation

Notice: Class Collection implements Countable and ArrayAccess.

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

License

MIT