array_add Github
The array_add
function adds a given key / value pair to the array if the given key doesn't already exist in the array.
$array = array('foo' => 'bar');
$array = array_add($array, 'key', 'value');
array_divide Github
The array_divide
function returns two arrays, one containing the keys, and the other containing the values of the original array.
$array = array('foo' => 'bar');
list($keys, $values) = array_divide($array);
array_dot Github
The array_dot
function flattens a multi-dimensional array into a single level array that uses "dot" notation to indicate depth.
$array = array('foo' => array('bar' => 'baz'));
$array = array_dot($array);
// array('foo.bar' => 'baz');
array_except Github
The array_except
method removes the given key / value pairs from the array.
$array = array_except($array, array('keys', 'to', 'remove'));
array_fetch Github
The array_fetch
method returns a flattened array containing the selected nested element.
$array = array(
array('developer' => array('name' => 'Taylor')),
array('developer' => array('name' => 'Dayle')),
);
$array = array_fetch($array, 'developer.name');
// array('Taylor', 'Dayle');
array_first Github
The array_first
method returns the first element of an array passing a given truth test.
$array = array(100, 200, 300);
$value = array_first($array, function($key, $value)
{
return $value >= 150;
});
A default value may also be passed as the third parameter:
$value = array_first($array, $callback, $default);
array_last Github
The array_last
method returns the last element of an array passing a given truth test.
$array = array(350, 400, 500, 300, 200, 100);
$value = array_last($array, function($key, $value)
{
return $value > 350;
});
// 500
A default value may also be passed as the third parameter:
$value = array_last($array, $callback, $default);
array_flatten Github
The array_flatten
method will flatten a multi-dimensional array into a single level.
$array = array('name' => 'Joe', 'languages' => array('PHP', 'Ruby'));
$array = array_flatten($array);
// array('Joe', 'PHP', 'Ruby');
array_forget Github
The array_forget
method will remove a given key / value pair from a deeply nested array using "dot" notation.
$array = array('names' => array('joe' => array('programmer')));
array_forget($array, 'names.joe');
array_get Github
The array_get
method will retrieve a given value from a deeply nested array using "dot" notation.
$array = array('names' => array('joe' => array('programmer')));
$value = array_get($array, 'names.joe');
Note: Want something like
array_get
but for objects instead? Useobject_get
.
array_only Github
The array_only
method will return only the specified key / value pairs from the array.
$array = array('name' => 'Joe', 'age' => 27, 'votes' => 1);
$array = array_only($array, array('name', 'votes'));
array_pluck Github
The array_pluck
method will pluck a list of the given key / value pairs from the array.
$array = array(array('name' => 'Taylor'), array('name' => 'Dayle'));
$array = array_pluck($array, 'name');
// array('Taylor', 'Dayle');
array_pull Github
The array_pull
method will return a given key / value pair from the array, as well as remove it.
$array = array('name' => 'Taylor', 'age' => 27);
$name = array_pull($array, 'name');
array_set Github
The array_set
method will set a value within a deeply nested array using "dot" notation.
$array = array('names' => array('programmer' => 'Joe'));
array_set($array, 'names.editor', 'Taylor');
array_sort
The array_sort
method sorts the array by the results of the given Closure.
$array = array(
array('name' => 'Jill'),
array('name' => 'Barry'),
);
$array = array_values(array_sort($array, function($value)
{
return $value['name'];
}));
array_where Github
Filter the array using the given Closure.
$array = array(100, '200', 300, '400', 500);
$array = array_where($array, function($key, $value)
{
return is_string($value);
});
// Array ( [1] => 200 [3] => 400 )
head Github
Return the first element in the array. Useful for method chaining in PHP 5.3.x.
$first = head($this->returnsArray('foo'));
last Github
Return the last element in the array. Useful for method chaining.
$last = last($this->returnsArray('foo'));
value Github
If the given value is a Closure
, return the value returned by the Closure
. Otherwise, return the value.
$value = value(function() { return 'bar'; });
with Github
Return the given object. Useful for method chaining constructors in PHP 5.3.x.
$value = with(new Foo)->doWork();