Skip to content
This repository has been archived by the owner on Jul 6, 2019. It is now read-only.
Anahkiasen edited this page Nov 24, 2012 · 32 revisions

Available methods

Methods yet to implement

  • reduce
  • shuffle
  • exclude
  • flatten
  • without
  • unique
  • merge
  • diff
  • intersection
  • indexOf
  • range
  • repeat

Informations about an array

Arrays::average

Computes the average value of an array

Arrays::average(array(1, 2, 3)) // Returns 2

Arrays::contains

Check if an item is in an array

Arrays::contains(array(1, 2, 3), 2) // Returns true

Arrays::has

Check if a key exists in an array

Arrays::has(array('foo' => 'bar'), 'foo') // Returns true

Arrays::matches

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
});

Arrays::matchesAny

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
});

Arrays::max

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;
});

Arrays::min

Similar to Arrays::max but for the lowest value

Arrays::size

Get the size of an array

Arrays::size(array(1, 2, 3)) // Returns 3

Arrays::sum

Computes the sum of an array

Arrays::sum(array(1, 2, 3)) // Returns 6

Array slicers

Arrays::first

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)

Arrays::last

Get the last value from an array

Arrays::last(array(1, 2, 3)) // Returns 3

Arrays::initial

Exclude the last X elements from an array

Arrays::initial(array(1, 2, 3), 1) // Returns array(1, 2)

Arrays::rest

Exclude the first X elements from an array

Arrays::rest(array(1, 2, 3), 2) // Returns 3

Get from an array

Arrays::find

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
});

Arrays::get

Get a value from an array using dot-notation

$array = underscore(array('foo' => array('bar' => 'ter')));
$array->get('foo.bar') // Return 'ter'

Arrays::pluck

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')

Arrays::clean

Remove all falsy values from an array

Arrays::clean(array(true, false, 0, 1, 'string', '')) // Returns array(true, 1, 'string')

Act upon an array

Arrays::at

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
});

Alter an array

Arrays::each

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)
});

Arrays::filter

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)
});

Arrays::reject

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)
});

Arrays::invoke

Invoke a function on all of an array's values

Arrays::invoke(array('   foo'), 'trim'); // Returns array('foo')

Arrays::toJSON

Converts an array to JSON

Arrays::toJSON(array(1, 2, 3)) // Returns [1, 2, 3]

Arrays::group

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))
})

Arrays::sort

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
Clone this wiki locally