Tests if an array is associative or not.
Parameters
Returns
Example
// Returns TRUE
Arr::is_assoc(array('username' => 'john.doe'));
// Returns FALSE
Arr::is_assoc('foo', 'bar');
Test if a value is an array with an additional check for array-like objects.
Parameters
Returns
Example
// Returns TRUE
Arr::is_array(array());
Arr::is_array(new ArrayObject);
// Returns FALSE
Arr::is_array(FALSE);
Arr::is_array('not an array!');
Arr::is_array(Database::instance());
Gets a value from an array using a dot separated path.
Parameters
Returns
Example
// Get the value of $array['foo']['bar']
$value = Arr::path($array, 'foo.bar');
// Using a wildcard "*" will search intermediate arrays and return an array.
// Get the values of "color" in theme
$colors = Arr::path($array, 'theme.*.color');
// Using an array of keys
$colors = Arr::path($array, array('theme', '*', 'color'));
Set a value on an array by path.
Parameters
Returns
Example
$data = ['level1' => 'level1a' => 'bar'];
// Add level2a = foo into level1
Arr::set_path($data, 'level1.level2a', 'foo');
// Overwrite level1a
Add::set_path($data, 'level1.level1a', 'foo');
Fill an array with a range of numbers.
Parameters
Returns
Example
// Fill an array with values 5, 10, 15, 20
$values = Arr::range(5, 20);
Retrieve a single key from an array. If the key does not exist in the array, the default value will be returned instead.
Parameters
Returns
Example
// Get the value "username" from $GLOBALS, if it exists
$username = Arr::get($GLOBALS, 'username');
// Get the value "sorting" from $GLOBALS, if it exists
$sorting = Arr::get($GLOBALS, 'sorting');
Retrieves multiple paths from an array. If the path does not exist in the array, the default value will be added instead.
Parameters
Returns
Example
// Get the values from command line arguments (-a "foo" -b "bar" -c "mamuph")
// Only "a" and "c" are retrieved.
$auth = Arr::extract(getops('a:b:c'), array('a', 'c'));
Example
// Get the value "level1.level2a" from $data (Full array path included)
$data = array('level1' => array('level2a' => 'value 1', 'level2b' => 'value 2'));
Arr::extract($data, array('level1.level2a', 'foo'));
Retrieves muliple single-key values from a list of arrays.
Parameters
Returns
Example
// Get all of the "id" values from a result
$ids = Arr::pluck($result, 'id');
Convert a multi-dimensional array into a single-dimensional array.
Parameters
Returns
Example
$array = array('set' => array('one' => 'something'), 'two' => 'other');
// The array will now be array('one' => 'something', 'two' => 'other');
$array = Arr::flatten($array);
Recursive version of array_map, applies one or more callbacks to all elements in an array, including sub-arrays.
Parameters
Returns
Example
// Apply "strip_tags" to every element in the array
$array = Arr::map('strip_tags', $array);
// Apply $this->filter to every element in the array
$array = Arr::map(array($this,'filter'), $array);
// Apply strip_tags and $this->filter to every element
$array = Arr::map(array('strip_tags',array($this,'filter')), $array);
Recursively merge two or more arrays. Values in an associative array overwrite previous values with the same key. Values in an indexed array are appended, but only when they do not already exist in the result.
Note that this does not work the same as array_merge_recursive!
Parameters
Returns
Example
$john = array('name' => 'john', 'children' => array('fred', 'paul', 'sally', 'jane'));
$mary = array('name' => 'mary', 'children' => array('jane'));
// John and Mary are married, merge them together
$john = Arr::merge($john, $mary);
// The output of $john will now be:
array('name' => 'mary', 'children' => array('fred', 'paul', 'sally', 'jane'))
Overwrites an array with values from input arrays. Keys that do not exist in the first array will not be added!
Parameters
Returns
Example
$a1 = array('name' => 'john', 'mood' => 'happy', 'food' => 'bacon');
$a2 = array('name' => 'jack', 'food' => 'tacos', 'drink' => 'beer');
// Overwrite the values of $a1 with $a2
$array = Arr::overwrite($a1, $a2);
// The output of $array will now be:
array('name' => 'jack', 'mood' => 'happy', 'food' => 'tacos')
Creates a callable function and parameter list from a string representation. Note that this function does not validate the callback string.
Parameters
Returns
Example
// Get the callback function and parameters
list($func, $params) = Arr::callback('Foo::bar(apple,orange)');
// Get the result of the callback
$result = call_user_func_array($func, $params);
Example
// Get the callback function and parameters
list($func, $params) = Arr::callback('Foo::bar(1,2,3,hey)');
// $Func = [0 => "Foo", 1 => "bar"]
var_dump($func);
// $params = [0 => "1", 1 => "2", 2 => "3", 3 => "hey"]
var_dump($params);
Parameters are always passed as string type.
Space separator between the parameters are interpreted as empty spaces.