\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 = array()) 

Create a new Dot instance

Parameters

mixed $items

add()

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

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

Parameters

array|integer|string $keys
mixed $value

all()

all() : array

Return all the stored items

Returns

array

clear()

clear(array|integer|string|null  $keys = null) 

Delete the contents of a given key or keys

Parameters

array|integer|string|null $keys

delete()

delete(array|integer|string  $keys) 

Delete the given key or keys

Parameters

array|integer|string $keys

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(integer|string|null  $key = null, mixed  $default = null) : mixed

Return the value of a given key

Parameters

integer|string|null $key
mixed $default

Returns

mixed

has()

has(array|integer|string  $keys) : boolean

Check if a given key or keys exists

Parameters

array|integer|string $keys

Returns

boolean

isEmpty()

isEmpty(array|integer|string|null  $keys = null) : boolean

Check if a given key or keys are empty

Parameters

array|integer|string|null $keys

Returns

boolean

merge()

merge(array|string|self  $key, array|self  $value = array()) 

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

mergeRecursive()

mergeRecursive(array|string|self  $key, array|self  $value = array()) 

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

mergeRecursiveDistinct()

mergeRecursiveDistinct(array|string|self  $key, array|self  $value = array()) 

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

pull()

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

Return the value of a given key and delete the key

Parameters

integer|string|null $key
mixed $default

Returns

mixed

push()

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

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

Parameters

mixed $key
mixed $value

replace()

replace(array|string|self  $key, array|self  $value = array()) 

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

Parameters

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

set()

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

Set a given key / value pair or pairs

Parameters

array|integer|string $keys
mixed $value

setArray()

setArray(mixed  $items) 

Replace all items with a given array

Parameters

mixed $items

setReference()

setReference(array  $items) 

Replace all items with a given array as a reference

Parameters

array $items

toJson()

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

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

Parameters

mixed $key
integer $options

Returns

string

offsetExists()

offsetExists(integer|string  $key) : boolean

Check if a given key exists

Parameters

integer|string $key

Returns

boolean

offsetGet()

offsetGet(integer|string  $key) : mixed

Return the value of a given key

Parameters

integer|string $key

Returns

mixed

offsetSet()

offsetSet(integer|string|null  $key, mixed  $value) 

Set a given value to the given key

Parameters

integer|string|null $key
mixed $value

offsetUnset()

offsetUnset(integer|string  $key) 

Delete the given key

Parameters

integer|string $key

count()

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

Return the number of items in a given key

Parameters

integer|string|null $key

Returns

integer

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, integer|string  $key) : boolean

Checks if the given key exists in the provided array.

Parameters

array $array

Array to validate

integer|string $key

The key to look for

Returns

boolean

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