ArrayList
Raxos\Collection\ArrayList is the default mutable, ordered list. It combines the ArrayListable operations with array access, iteration, counting and JSON serialization. Most other list types in the package extend it.
class ArrayList implements
ArrayListInterface,
MutableArrayListInterface,
DebuggableInterface,
JsonSerializable,
SerializableInterfaceConstruction
__construct
public function __construct(array $data = [])Creates a list from a plain PHP array.
of
public static function of(iterable $items): staticCreates a new instance from any iterable. Traversable sources are converted with iterator_to_array, list arrays are reindexed, and on validated subclasses every item is checked with validateItem.
Mutating operations
append
public function append(mixed $item): voidAdds an item to the end of the list.
prepend
public function prepend(mixed $item): voidAdds an item to the start of the list.
pop
public function pop(): mixedRemoves and returns the last item.
shift
public function shift(): mixedRemoves and returns the first item.
Transformation operations
These come from the ArrayListable trait. Each returns a new instance of the same class and leaves the receiver untouched.
map
public function map(callable $fn): staticMaps every item to a new value.
filter
public function filter(callable $predicate): staticKeeps only items for which the predicate returns true, then reindexes.
reduce
public function reduce(callable $fn, mixed $initial = null): mixedReduces the list to a single value.
first
public function first(?callable $predicate = null, mixed $default = null): mixedReturns the first item, optionally the first matching a predicate, or the default.
last
public function last(?callable $predicate = null, mixed $default = null): mixedReturns the last item, optionally the last matching a predicate, or the default.
chunk
public function chunk(int $size): staticSplits the list into a list of lists of the given size.
groupBy
public function groupBy(callable $fn): staticGroups items into sublists keyed by the result of the callback.
sort
public function sort(callable $compare): staticReturns a sorted copy using the comparator.
slice
public function slice(int $offset, ?int $length = null): staticReturns a portion of the list.
convertTo
public function convertTo(string $implementation): ArrayListInterfaceRebuilds the list as another ArrayListInterface implementation, for example StringArrayList.
Iteration and key/value helpers
each
public function each(callable $fn): staticRuns the callback for every item and key purely for side effects and returns the same instance, unlike map.
keys
public function keys(): staticReturns a new list of the current keys.
values
public function values(): staticReturns a new list of the current values, dropping the keys.
firstKey
public function firstKey(): string|int|nullReturns the key of the first item, or null when the list is empty.
lastKey
public function lastKey(): string|int|nullReturns the key of the last item, or null when the list is empty.
Reshaping helpers
column
public function column(string|int ...$columns): staticExtracts a single field from every array or object item. Extra columns drill one level deeper into the previous result.
collapse
public function collapse(): staticFlattens one level of nested arrays or nested list items into a single flat list.
only
public function only(array $keys): staticReduces every item to the given keys: array items are narrowed, objects that define their own only method are delegated to, and other items pass through unchanged.
clone
public function clone(): staticReturns a fresh instance of the same class holding the same data.
The trait also provides every, some, contains, search, merge, diff, unique, reverse, shuffle, splice, isEmpty and isNotEmpty.
Access and serialization
toArray() returns the underlying array, count() returns the item count, getIterator() yields an ArrayIterator, and jsonSerialize() returns the data for json_encode.
Example
<?php
declare(strict_types=1);
use Raxos\Collection\ArrayList;
$orders = ArrayList::of([
['id' => 1, 'total' => 40],
['id' => 2, 'total' => 15],
['id' => 3, 'total' => 90],
]);
$largeTotals = $orders
->filter(static fn(array $order): bool => $order['total'] >= 40)
->map(static fn(array $order): int => $order['total']);
$largeTotals->reduce(static fn(int $carry, int $total): int => $carry + $total, 0); // 130See also the array lists concept page.