Arrays Component provide a fluent, object-oriented interface for working with arrays, allowing you to chain multiple arrays operations together using a more readable syntax compared to traditional PHP arrays functions.

Installation

With Composer

composer require atomastic/arrays

Usage

use Atomastic\Arrays\Arrays;

// Using public method __construct()
$arrays = new Arrays();

// Using public static method create()
$arrays = Arrays::create();

// Using global helper function arrays() alias to Arrays::create()
$arrays = arrays();

Extending

Arrays are "macroable", which allows you to add additional methods to the Arrays class at run time. For example, the following code adds a customMethod method to the Arrays class:

use Atomastic\Arrays\Arrays;
use Atomastic\Macroable\Macroable;

Arrays::macro('customMethod', function($arg1 = 1, $arg2 = 1) {
    return $this->count() + $arg1 + $arg2;
});

$arrays = new Arrays([1, 2, 3]);

echo $arrays->customMethod(1, 2);
echo $arrays->customMethod();

The above example will output:

6
5

Methods

all Get all items from stored array.
append Push an item into the end of an array.
chunk Create a chunked version of current array.
column Get the values of a single column from an arrays items.
combine Create an array using the current array as keys and the other array as values.
copy Creates a new Arrays object with the same items.
count Return the number of items in a given key.
create Create a new arrayable object from the given elements.
createFromJson Create a new arrayable object from the given JSON string.
createFromString Create a new arrayable object from the given string.
createWithRange Create a new arrayable object from the given string.
current Gets the element of the array at the current internal iterator position.
customSortKeys Sorts the array keys with a user-defined comparison function and maintain index association.
customSortValues Sorts the array values with a user-defined comparison function and maintain index association.
dd Dumps the arrays items using the given function (print_r by default) and die.
delete Deletes an array value using "dot notation".
diff Compute the current array values which not present in the given one.
divide Divide an array into two arrays.
dot Flatten a multi-dimensional associative array with dots.
dump Dumps the arrays items using the given function (print_r by default).
every Verifies that all elements pass the test of the given callback.
except Return slice of an array with just a given keys.
extract Extract the items from the current array using "dot" notation for further manipulations.
filter Filter the current array for elements satisfying the predicate $callback function.
first Get the first value from the current array.
firstKey Get the first key from the current array.
flip Exchanges all keys of current array with their associated values.
flush Flush all values from the array.
get Get an item from an array using "dot" notation.
getIterator Create a new iterator from an ArrayObject instance.
getValues Return an array of all values stored array.
groupBy Groups the array items by a given key.
has Checks if the given dot-notated key exists in the array.
indexOf Alias of search() method. Search for a given item and return the index of its first occurrence.
intersect Compute the current array values which present in the given one.
intersectAssoc Compute the current array values with additional index check.
intersectKey Compute the current array using keys for comparison which present in the given one.
isAssoc Determines if an array is associative.
isEmpty Check whether the array is empty or not.
isEqual Check if the current array is equal to the given $array or not.
last Get the last value from the current array.
lastKey Get the last key from the current array.
limit Extract a slice of the current array with offset 0 and specific length.
map Apply the given $callback function to the every element of the current array, collecting the results.
merge Merge the current array with the provided one.
next Moves the internal iterator position to the next element and returns this element.
nth Extract array items with every nth item from the array.
offset Extract a slice of the current array with specific offset.
offsetExists Whether an offset exists.
offsetGet Offset to retrieve.
offsetSet Assign a value to the specified offset.
offsetUnset Unset an offset.
only Return slice of an array with just a given keys.
pad Pad the current array to the specified size with a given value.
pipe Passes the array to the given callback and return the result.
prepend Push an item into the beginning of an array.
prev Rewind the internal iterator position and returns this element.
product Calculate the product of values in the current array.
pull Get a value from the array, and remove it.
random Returns one or a specified number of items randomly from the array.
reduce Reduce the array to a single value iteratively combining all values using $callback.
reindex Create a numerically re-indexed array based on the current array.
replace Replace values in the current array with values in the given one that have the same key.
reverse Reverse the values order of the current array.
search Searches the array for a given value and returns the first corresponding key if successful.
set Set an array item to a given value using "dot" notation.
shuffle Shuffle the given array and return the result.
skip Skip the first count items.
slice Extract a slice of the current array.
sort Sorts a associative array by a certain sub key.
sortBy Sorts a associative array by a certain key.
sortKeys Sorts array by keys.
sum Calculate the sum of values in the current array.
toArray Get all items from stored array and convert them to array.
toJson Convert the current array to JSON.
toQuery Convert the array into a query string.
toString Convert the current array to string recursively implodes an array with optional key inclusion.
undot Expands a dot notation array into a full multi-dimensional array.
unique Remove duplicate values from the current array.
walk Apply the given function to the every element of the current array, discarding the results.
where Filters the array items by a given condition.
whereBetween Filters the array items by the given key is between the given values.
whereContains Filters the array items by the given key is contains given value.
whereEndsWith Filters the array items by the given key is ends with given value.
whereEqual Filters the array items by the given key is equal given value.
whereGreater Filters the array items by the given key is greater the given value.
whereGreaterOrEqual Filters the array items by the given key is greater or equal the given value.
whereIn Filters the array items by the given key value pair.
whereLess Filters the array items by the given key is less the given value.
whereLessOrEqual Filters the array items by the given key is less or equal the given value.
whereNewer Filters the array items by the given key is newer given value.
whereNotBetween Filters the array items by the given key is not between the given values.
whereNotContains Filters the array items by the given key is not contains given value.
whereNotEqual Filters the array items by the given key is not equal given value.
whereNotIn Filters the array items by the given key value pair.
whereNotRegexp Filters the array items by the given key is not matches to given regexp.
whereOlder Filters the array items by the given key is older given value.
whereRegexp Filters the array items by the given key is matches to given regexp.
whereStartsWith Filters the array items by the given key is starts with given value.
Edit this page on GitHub

Getting Started

Methods

  • all Get all items from stored array.
  • append Push an item into the end of an array.
  • chunk Create a chunked version of current array.
  • column Get the values of a single column from an arrays items.
  • combine Create an array using the current array as keys and the other array as values.
  • copy Creates a new Arrays object with the same items.
  • count Return the number of items in a given key.
  • create Create a new arrayable object from the given elements.
  • createFromJson Create a new arrayable object from the given JSON string.
  • createFromString Create a new arrayable object from the given string.
  • createWithRange Create a new arrayable object from the given string.
  • current Gets the element of the array at the current internal iterator position.
  • customSortKeys Sorts the array keys with a user-defined comparison function and maintain index association.
  • customSortValues Sorts the array values with a user-defined comparison function and maintain index association.
  • dd Dumps the arrays items using the given function (print_r by default) and die.
  • delete Deletes an array value using "dot notation".
  • diff Compute the current array values which not present in the given one.
  • divide Divide an array into two arrays.
  • dot Flatten a multi-dimensional associative array with dots.
  • dump Dumps the arrays items using the given function (print_r by default).
  • every Verifies that all elements pass the test of the given callback.
  • except Return slice of an array with just a given keys.
  • extract Extract the items from the current array using "dot" notation for further manipulations.
  • filter Filter the current array for elements satisfying the predicate $callback function.
  • first Get the first value from the current array.
  • firstKey Get the first key from the current array.
  • flip Exchanges all keys of current array with their associated values.
  • flush Flush all values from the array.
  • get Get an item from an array using "dot" notation.
  • getIterator Create a new iterator from an ArrayObject instance.
  • getValues Return an array of all values stored array.
  • groupBy Groups the array items by a given key.
  • has Checks if the given dot-notated key exists in the array.
  • indexOf Alias of search() method. Search for a given item and return the index of its first occurrence.
  • intersect Compute the current array values which present in the given one.
  • intersectAssoc Compute the current array values with additional index check.
  • intersectKey Compute the current array using keys for comparison which present in the given one.
  • isAssoc Determines if an array is associative.
  • isEmpty Check whether the array is empty or not.
  • isEqual Check if the current array is equal to the given $array or not.
  • last Get the last value from the current array.
  • lastKey Get the last key from the current array.
  • limit Extract a slice of the current array with offset 0 and specific length.
  • map Apply the given $callback function to the every element of the current array, collecting the results.
  • merge Merge the current array with the provided one.
  • next Moves the internal iterator position to the next element and returns this element.
  • nth Extract array items with every nth item from the array.
  • offset Extract a slice of the current array with specific offset.
  • offsetExists Whether an offset exists.
  • offsetGet Offset to retrieve.
  • offsetSet Assign a value to the specified offset.
  • offsetUnset Unset an offset.
  • only Return slice of an array with just a given keys.
  • pad Pad the current array to the specified size with a given value.
  • pipe Passes the array to the given callback and return the result.
  • prepend Push an item into the beginning of an array.
  • prev Rewind the internal iterator position and returns this element.
  • product Calculate the product of values in the current array.
  • pull Get a value from the array, and remove it.
  • random Returns one or a specified number of items randomly from the array.
  • reduce Reduce the array to a single value iteratively combining all values using $callback.
  • reindex Create a numerically re-indexed array based on the current array.
  • replace Replace values in the current array with values in the given one that have the same key.
  • reverse Reverse the values order of the current array.
  • search Searches the array for a given value and returns the first corresponding key if successful.
  • set Set an array item to a given value using "dot" notation.
  • shuffle Shuffle the given array and return the result.
  • skip Skip the first count items.
  • slice Extract a slice of the current array.
  • sort Sorts a associative array by a certain sub key.
  • sortBy Sorts a associative array by a certain key.
  • sortKeys Sorts array by keys.
  • sum Calculate the sum of values in the current array.
  • toArray Get all items from stored array and convert them to array.
  • toJson Convert the current array to JSON.
  • toQuery Convert the array into a query string.
  • toString Convert the current array to string recursively implodes an array with optional key inclusion.
  • undot Expands a dot notation array into a full multi-dimensional array.
  • unique Remove duplicate values from the current array.
  • walk Apply the given function to the every element of the current array, discarding the results.
  • where Filters the array items by a given condition.
  • whereBetween Filters the array items by the given key is between the given values.
  • whereContains Filters the array items by the given key is contains given value.
  • whereEndsWith Filters the array items by the given key is ends with given value.
  • whereEqual Filters the array items by the given key is equal given value.
  • whereGreater Filters the array items by the given key is greater the given value.
  • whereGreaterOrEqual Filters the array items by the given key is greater or equal the given value.
  • whereIn Filters the array items by the given key value pair.
  • whereLess Filters the array items by the given key is less the given value.
  • whereLessOrEqual Filters the array items by the given key is less or equal the given value.
  • whereNewer Filters the array items by the given key is newer given value.
  • whereNotBetween Filters the array items by the given key is not between the given values.
  • whereNotContains Filters the array items by the given key is not contains given value.
  • whereNotEqual Filters the array items by the given key is not equal given value.
  • whereNotIn Filters the array items by the given key value pair.
  • whereNotRegexp Filters the array items by the given key is not matches to given regexp.
  • whereOlder Filters the array items by the given key is older given value.
  • whereRegexp Filters the array items by the given key is matches to given regexp.
  • whereStartsWith Filters the array items by the given key is starts with given value.