-
Notifications
You must be signed in to change notification settings - Fork 88
Arrays
Available methods
-
Informations about an array
- average
- contains
- matches
- matchesAny
- max
- min
- size
- sum
-
Get from an array
- find
- first
- get
- last
- pluck
-
Act upon an array
- at
-
Alter an array
- each
- filter
- reject
- invoke
Methods yet to implement
- reduce
- group
- sort
- shuffle
- exclude
- clean
- flatten
- without
- unique
- merge
- diff
- intersection
- indexOf
- range
- repeat
Computes the average value of an array
Arrays::average(array(1, 2, 3)) // Returns 2
Check if an item is in an array
Arrays::contains(array(1, 2, 3), 2) // Returns true
Check if all items in an array match a truth test
Arrays::matches(array(1, 2, 3), function($value) {
return $value % 2 == 0; // Returns false
});
Same than above but returns true if at least one item matches
Arrays::matchesAny(array(1, 2, 3), function($value) {
return $value % 2 == 0; // Returns true
});
Get the maximum value from an array. A closure can be passed to evaluate the values a certain way
Arrays::max(array(1, 2, 3)) // Returns 3
Arrays::max(array(1, 2, 3), function($value) {
return $value * -1;
});
Similar to Arrays::max but for the lowest value
Get the size of an array
Arrays::size(array(1, 2, 3)) // Returns 3
Computes the sum of an array
Arrays::sum(array(1, 2, 3)) // Returns 6
Find the first value in an array that passes a truth test
Arrays::find(array(1, 2, 3), function($value) {
return $value % 2 == 0; // Returns 2
});
Get the first value from an array. You can also specify a number of elements to return
Arrays::first(array(1, 2, 3)) // Returns 1
Arrays::first(array(1, 2, 3), 2) // Returns array(1, 2)
Get the last value from an array
Arrays::last(array(1, 2, 3)) // Returns 3
Get a value from an array using dot-notation
$array = underscore(array('foo' => array('bar' => 'ter')));
$array->get('foo.bar') // Return 'ter'
Pluck a column from an array
$array = array(
array('foo' => 'bar', 'bis' => 'ter'),
array('foo' => 'bar', 'bis' => 'ter'),
);
Arrays::pluck($array, 'foo'); // Returns array('bar', 'bar')
Iterate over an array to execute a callback at each loop
$multiplier = 3;
Arrays::at(array(1, 2, 3), function($value) use ($multiplier) {
echo $value * $multiplier; // Prints out 3, 6, 9
});
Iterate over an array and apply a callback to each value
Arrays::each(array(1, 2, 3), function($value) {
return $value * 3; // Return array(3, 6, 9)
});
Find all values in an array that passes a truth test
Arrays::filter(array(1, 2, 3), function($value) {
return $value % 2 != 0; // Returns array(1, 3)
});
Find all values in an array that are rejected by a truth test
Arrays::filter(array(1, 2, 3), function($value) {
return $value % 2 != 0; // Returns array(2)
});
Invoke a function on all of an array's values
Arrays::invoke(array(' foo'), 'trim'); // Returns array('foo')