Class ArrayDotNotation
An easier way to deal with complex PHP arrays
Methods summary
public
|
#
__construct( array $data = [] )
An alias for setData
Parameters
- $data
- Array of data that will be accessed via dot notation.
Since
1.0.0
See
|
public
array
|
#
getData( )
returns the array
returns the array that the dot notation has been used on.
Returns
array Array of data that will be accessed via dot notation.
Since
1.0.0
|
public
|
#
setData( array $data )
sets the array that the dot notation will be used on.
sets the array that the dot notation will be used on.
Parameters
- $data
- Array of data that will be accessed via dot notation.
Returns
$this
Since
1.0.0
|
public
mixed
|
#
get( string $dotNotation, mixed $defaultValue = null )
Retrieves a value from an array structure as defined by a dot notation string
Retrieves a value from an array structure as defined by a dot notation string
Returns a value from an array.
Parameters
- $dotNotation
- a string of keys concatenated together by a dot that represent different levels of an array
- $defaultValue
- value to return if the dot notation does not find a valid key
Returns
mixed value found via dot notation in the array of data
Since
1.0.0
|
protected
mixed
|
#
recursiveGet( array $data, array $keys, mixed $defaultValue )
Recursively works though an array level by level until the final key is found. Once key is found the keys value is returned.
Recursively works though an array level by level until the final key is found. Once key is found the keys value is returned.
Parameters
- $data
- array that has value to be retrieved
- $keys
- array of keys for each level of the array
- $defaultValue
- value to return when a key is not found
Returns
mixed value that the keys find in the data array
Since
1.0.0
|
public
|
#
set( string $dotNotation, mixed $value )
sets a value into a complicated array data structure
sets a value into a complicated array data structure
Places a value into a tiered array. A dot notation of "one.two.three"
would place the value at ['one' => ['two' => ['three' => 'valueHere' ]]]
If the array does not exist it will be added. Indexed arrays can be used,
and referenced by number. i.e. "one.0" would return the first item of the
one array.
Parameters
- $dotNotation
- dot notation representation of keys of where to set a value
- $value
- any value to be stored with in a key structure of dot notation
Returns
$this
Throws
Since
1.0.0
|
protected
|
#
recursiveSet( array & $data, array $keys, mixed $value )
Transverses the keys array until it reaches the appropriate key in the data array and sets that key to the value.
If the keys don't exist they are created.
Transverses the keys array until it reaches the appropriate key in the data array and sets that key to the value.
If the keys don't exist they are created.
Parameters
- $data
- data to be traversed
- $keys
- the remaining keys of focus for the data array
- $value
- the value to be placed at the final key
Throws
Since
1.0.0
|
public
|
#
merge( string $dotNotation, array $value )
Merges two arrays together over writing existing values with new values, while adding new array structure to the data
Merges two arrays together over writing existing values with new values, while adding new array structure to the data
Parameters
- $dotNotation
- dot notation representation of keys of where to set a value
- $value
- array to be merged with an existing array
Returns
$this
Throws
Since
1.1.0
|
public
|
#
remove( string $dotNotation )
Removes data from the array
Removes data from the array
Parameters
- $dotNotation
- dot notation representation of keys of where to remove a value
Returns
$this
Since
1.1.0
|
protected
|
#
recursiveRemove( array & $data, array $keys )
Transverses the keys array until it reaches the appropriate key in the data array and then deletes the value from the key.
Transverses the keys array until it reaches the appropriate key in the data array and then deletes the value from the key.
Parameters
- $data
- data to be traversed
- $keys
- the remaining keys of focus for the data array
Throws
Since
1.1.0
|