-
Notifications
You must be signed in to change notification settings - Fork 88
Arrays
Available methods
- Informations about an array
- Array slicers
- Get from an array
- Generate arrays
- Act upon an array
- Alter an array
Methods yet to implement
- exclude
- intersection
- unique
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 a key exists in an array
Arrays::has(array('foo' => 'bar'), 'foo') // 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
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
Exclude the last X elements from an array
Arrays::initial(array(1, 2, 3), 1) // Returns array(1, 2)
Exclude the first X elements from an array
Arrays::rest(array(1, 2, 3), 2) // Returns 3
Computes the difference between multiple arrays
Arrays::diff(array(1, 2, 3), array(1, 5)) // Returns array(2, 3)
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 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')
Remove all falsy values from an array
Arrays::clean(array(true, false, 0, 1, 'string', '')) // Returns array(true, 1, 'string')
Get a random value from an array
Arrays::random(array(1, 2, 3)) // Returns 1, 2 or 3
Returns the array without all instances of the given values
$array = array('foo', 'foo', 'bar', 'bis', 'ter')
Arrays::without($array, 'foo', 'bis') // Returns array('bar', 'ter')
Generate an array from a range
Arrays::range(5) // Returns array(1, 2, 3, 4, 5)
Arrays::range(-2, 2) // Returns array(-2, -1, 0, 1, 2)
Arrays::range(1, 10, 2) // Returns array(1, 3, 5, 7, 9)
Fill an array with $times times the $data
Arrays::repeat('foo', 3) // Returns array('foo', 'foo', 'foo')
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
});
Append a value to an array
Arrays::append(array(1, 2, 3), 4) // Returns array(1, 2, 3, 4)
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 If no closure specified, simply cleans the array (see: Arrays::clean)
Arrays::filter(array(1, 2, 3), function($value) {
return $value % 2 != 0; // Returns array(1, 3)
});
Flattens an array to dot notation
$array = array('foo' => array('bar' => array('bis' => 'ter')))
Arrays::flatten($array) // Returns array('foo.bar.bis' => 'ter')
Group an array by the results of a closure Instead of a closure a property name can be passed to group by it
Arrays::group(array(1, 2, 3, 4, 5), function($value) {
return $value % 2 == 0; // Returns array(array(1, 3, 5), array(2, 4))
})
Invoke a function on all of an array's values
Arrays::invoke(array(' foo'), 'trim'); // Returns array('foo')
Implodes an array into a string
Arrays::implode(array('foo', 'bar'), ', '); // Returns "foo, bar"
Merge on or more arrays together
Arrays::merge(array(1, 2), array('foo' => 'bar')) // Returns array(1, 2, 'foo' => 'bar')
Arrays::from(array(1, 2))->merge(array('foo' => 'bar')) // Same thing
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)
});
Remove values from arrays using dot notation
Arrays::remove($articles, '1.author.name') // Will unset $articles[1]['author']['name']
});
Remove the first value from an array
Arrays::removeFirst(array(1, 2, 3)) // Will return [2, 3]
Remove the last value from an array
Arrays::removeLast(array(1, 2, 3)) // Will return [1, 2]
Replace in the array values
Arrays::replaceValue(array('foo', 'foo', 'bar'), 'foo', 'bar') // Will return ['foo, 'foo', 'foo']
Prepend a value to an array
Arrays::prepend(array(1, 2, 3), 4) // Returns array(4, 1, 2, 3)
Set an value in an array using dot notation
Arrays::set(array(), 'foo.bar', 'bis') // Returns array('foo' => array('bar' => 'bis'))
Sort an array The second argument can be a closure returning what to sort by, or a property name The third argument is the direction (asc or desc)
Arrays::sort(array(5, 3, 1, 2 ,4), null, 'desc') // Returns array(5, 4, 3, 2, 1)
Arrays::sort($articles, function($article) {
return $article->name; // Returns article sorted by name
});
Arrays::sort($articles, 'author.name', 'desc') // Returns articles sorted by author name, DESC
Sort an array by keys
Arrays::sortKeys(array('z' => 0, 'b' => 1, 'r' => 2)) // Returns array('b' => 1, 'r' => 2, 'z' => 0)
Arrays::sortKeys(array('z' => 0, 'b' => 1, 'r' => 2), 'desc') // Returns array('z' => 0, 'r' => 2, 'b' => 1)