\AdbarDot

Dot

This class provides a dot notation access and helper functions for working with arrays of data. Inspired by Laravel Collection.

Summary

Methods
Properties
Constants
__construct()
add()
all()
clear()
delete()
flatten()
get()
has()
isEmpty()
merge()
mergeRecursive()
mergeRecursiveDistinct()
pull()
push()
replace()
set()
setArray()
setReference()
toJson()
offsetExists()
offsetGet()
offsetSet()
offsetUnset()
count()
getIterator()
jsonSerialize()
No public properties found
No constants found
exists()
getArrayItems()
arrayMergeRecursiveDistinct()
$items
N/A
No private methods found
No private properties found
N/A

Properties

$items

$items : array

The stored items

Type

array

Methods

__construct()

__construct(mixed  $items = []) : mixed

Create a new Dot instance

Parameters

mixed $items

Returns

mixed —

add()

add(array|int|string  $keys, mixed  $value = null) : mixed

Set a given key / value pair or pairs if the key doesn't exist already

Parameters

array|int|string $keys
mixed $value

Returns

mixed —

all()

all() : array

Return all the stored items

Returns

array —

clear()

clear(array|int|string|null  $keys = null) : mixed

Delete the contents of a given key or keys

Parameters

array|int|string|null $keys

Returns

mixed —

delete()

delete(array|int|string  $keys) : mixed

Delete the given key or keys

Parameters

array|int|string $keys

Returns

mixed —

flatten()

flatten(string  $delimiter = '.', array|null  $items = null, string  $prepend = '') : array

Flatten an array with the given character as a key delimiter

Parameters

string $delimiter
array|null $items
string $prepend

Returns

array —

get()

get(int|string|null  $key = null, mixed  $default = null) : mixed

Return the value of a given key

Parameters

int|string|null $key
mixed $default

Returns

mixed —

has()

has(array|int|string  $keys) : bool

Check if a given key or keys exists

Parameters

array|int|string $keys

Returns

bool —

isEmpty()

isEmpty(array|int|string|null  $keys = null) : bool

Check if a given key or keys are empty

Parameters

array|int|string|null $keys

Returns

bool —

merge()

merge(array|string|self  $key, array|self  $value = []) : mixed

Merge a given array or a Dot object with the given key or with the whole Dot object

Parameters

array|string|self $key
array|self $value

Returns

mixed —

mergeRecursive()

mergeRecursive(array|string|self  $key, array|self  $value = []) : mixed

Recursively merge a given array or a Dot object with the given key or with the whole Dot object.

Duplicate keys are converted to arrays.

Parameters

array|string|self $key
array|self $value

Returns

mixed —

mergeRecursiveDistinct()

mergeRecursiveDistinct(array|string|self  $key, array|self  $value = []) : mixed

Recursively merge a given array or a Dot object with the given key or with the whole Dot object.

Instead of converting duplicate keys to arrays, the value from given array will replace the value in Dot object.

Parameters

array|string|self $key
array|self $value

Returns

mixed —

pull()

pull(int|string|null  $key = null, mixed  $default = null) : mixed

Return the value of a given key and delete the key

Parameters

int|string|null $key
mixed $default

Returns

mixed —

push()

push(mixed  $key, mixed  $value = null) : mixed

Push a given value to the end of the array in a given key

Parameters

mixed $key
mixed $value

Returns

mixed —

replace()

replace(array|string|self  $key, array|self  $value = []) : mixed

Replace all values or values within the given key with an array or Dot object

Parameters

array|string|self $key
array|self $value

Returns

mixed —

set()

set(array|int|string  $keys, mixed  $value = null) : mixed

Set a given key / value pair or pairs

Parameters

array|int|string $keys
mixed $value

Returns

mixed —

setArray()

setArray(mixed  $items) : mixed

Replace all items with a given array

Parameters

mixed $items

Returns

mixed —

setReference()

setReference(array  $items) : mixed

Replace all items with a given array as a reference

Parameters

array $items

Returns

mixed —

toJson()

toJson(mixed  $key = null, int  $options) : string

Return the value of a given key or all the values as JSON

Parameters

mixed $key
int $options

Returns

string —

offsetExists()

offsetExists(int|string  $key) : bool

Check if a given key exists

Parameters

int|string $key

Returns

bool —

offsetGet()

offsetGet(int|string  $key) : mixed

Return the value of a given key

Parameters

int|string $key

Returns

mixed —

offsetSet()

offsetSet(int|string|null  $key, mixed  $value) : mixed

Set a given value to the given key

Parameters

int|string|null $key
mixed $value

Returns

mixed —

offsetUnset()

offsetUnset(int|string  $key) : mixed

Delete the given key

Parameters

int|string $key

Returns

mixed —

count()

count(int|string|null  $key = null) : int

Return the number of items in a given key

Parameters

int|string|null $key

Returns

int —

getIterator()

getIterator() : \ArrayIterator

Get an iterator for the stored items

Returns

\ArrayIterator —

jsonSerialize()

jsonSerialize() : array

Return items for JSON serialization

Returns

array —

exists()

exists(array  $array, int|string  $key) : bool

Checks if the given key exists in the provided array.

Parameters

array $array

Array to validate

int|string $key

The key to look for

Returns

bool —

getArrayItems()

getArrayItems(mixed  $items) : array

Return the given items as an array

Parameters

mixed $items

Returns

array —

arrayMergeRecursiveDistinct()

arrayMergeRecursiveDistinct(array  $array1, array  $array2) : array

Merges two arrays recursively. In contrast to array_merge_recursive, duplicate keys are not converted to arrays but rather overwrite the value in the first array with the duplicate value in the second array.

Parameters

array $array1

Initial array to merge

array $array2

Array to recursively merge

Returns

array —