ake
Array/Object value normalizer.
public ake({Array})
Returns a value from an array or a property from an object, if it exists. If it doesn't exist a default value can be specified. Otherwise null is returned.
This helps prevent array key not found errors in the PHP interpreter.
Keys may be specified using dot-notation. This allows ake to be called only once instead of for each element in a reference chain. For example, you can call ake($myarray, 'object.child.other');
and each reference will be recursed into if it exists. If at any step the child does not exist (or is empty if $non_empty === TRUE
) then execution will stop and return the default value. This will also handle things if the child is not an array or object.
If the key contains round or square brackets, then this is taken as a search parameter, allowing the specified element to be search for child elements that match the search criteria. This search parameter can, and is actually designed to, be used with dot-notation. So for example, you can call ake($myarray, 'items(type.id=1).name')
to find an element in the items
sub-element of $myarray
that has it's own type
element with another sub-element of id
with a value that matches 1
. As you can imagine, this allows quite a power way of accessing sub-elements of arrays/objects using a simple dot-notation search parameter.
The $key
parameter can also be an array of keys. In this case, the array will be searched for each key and the first value found will be returned. This is handy if you need a value that could be stored under multiple possible key names.
Parameters
Parameter | Type | Description |
---|---|---|
$array | mixed | The array to search. Objects with public properties are also supported. |
$key | mixed | The array key or object property name to look for. The following key specifications |
are supported:
* _string_ - Single key.
* _string_ - Dot notation key for decending into multi-dimensional arrays/objects.
* _array_ - Array of keys to search for where the first found is returned. |
| $default
| mixed
| An optional default value to return if the key or property does not exist. This default can also be a callback function (closure). If so, the default value will be the value returned by the closure. Usefull of using objects as defaults. | | $non_empty
| bool
| indicates that empty values, such as empty arrays and strings should be treated as NULL, even if they exist as elements in the array/object |
Generated by Hazaar API Doc Generator