This document provides a complete reference for all JsArray methods and properties.
- Creation Methods
- Properties
- Transformation Methods
- Search Methods
- Access Methods
- Manipulation Methods
- Conversion Methods
- Mode Conversion Methods
Create an immutable JsArray from a PHP array.
public static function from(array $items): selfExample:
$array = JsArray::from([1, 2, 3]);
// Returns immutable JsArrayCreate an immutable JsArray from individual arguments.
public static function of(mixed ...$items): selfExample:
$array = JsArray::of(1, 2, 3);
// Returns [1, 2, 3]Create a mutable JsArray for high-performance operations.
public static function mutable(array $items): selfExample:
$array = JsArray::mutable([1, 2, 3]);
// Operations modify in placeAlias for mutable().
public static function createMutable(array $items): selfGet the number of items in the array (read-only).
public readonly int $lengthExample:
$array = JsArray::from([1, 2, 3]);
echo $array->length; // 3Check if the array is in mutable mode.
public readonly bool $isMutableExample:
$mutable = JsArray::mutable([1, 2, 3]);
echo $mutable->isMutable; // trueCheck if the array is in immutable mode.
public readonly bool $isImmutableExample:
$immutable = JsArray::from([1, 2, 3]);
echo $immutable->isImmutable; // trueCreate a new array with results of calling callback on every element.
public function map(callable $callback): selfParameters:
$callback: Function with signaturefn($value),fn($value, $index), orfn($value, $index, $array)
Returns: New JsArray (or same instance in mutable mode)
Example:
$array = JsArray::from([1, 2, 3]);
$result = $array->map(fn($n) => $n * 2);
// Returns [2, 4, 6]Create a new array with elements that pass the test.
public function filter(callable $callback): selfParameters:
$callback: Function that returns true to keep element
Returns: New JsArray with re-indexed numeric keys
Example:
$array = JsArray::from([1, 2, 3, 4, 5]);
$result = $array->filter(fn($n) => $n % 2 === 0);
// Returns [2, 4]Reduce the array to a single value.
public function reduce(callable $callback, mixed $initial = null): mixedParameters:
$callback: Function with signaturefn($accumulator, $value),fn($accumulator, $value, $index), orfn($accumulator, $value, $index, $array)$initial: Initial accumulator value (optional)
Returns: The reduced value
Example:
$array = JsArray::from([1, 2, 3, 4]);
$sum = $array->reduce(fn($acc, $n) => $acc + $n, 0);
// Returns 10Flatten the array by one or more levels.
public function flat(int $depth = 1): selfParameters:
$depth: How many levels to flatten (default: 1)
Returns: New JsArray with flattened structure
Example:
$array = JsArray::from([1, [2, [3, 4]], 5]);
$flat = $array->flat();
// Returns [1, 2, [3, 4], 5]
$flat2 = $array->flat(2);
// Returns [1, 2, 3, 4, 5]Map each element and flatten by one level.
public function flatMap(callable $callback): selfEquivalent to: $array->map($callback)->flat(1)
Example:
$array = JsArray::from([1, 2, 3]);
$result = $array->flatMap(fn($n) => [$n, $n * 2]);
// Returns [1, 2, 2, 4, 3, 6]Extract a portion of the array.
public function slice(int $start, ?int $end = null): selfParameters:
$start: Start index (negative counts from end)$end: End index (optional, negative counts from end)
Returns: New JsArray with re-indexed numeric keys
Example:
$array = JsArray::from([1, 2, 3, 4, 5]);
$result = $array->slice(1, 3);
// Returns [2, 3]
$result2 = $array->slice(-2);
// Returns [4, 5]Change contents by removing/replacing elements.
public function splice(int $start, ?int $deleteCount = null, array $items = []): arrayParameters:
$start: Start position$deleteCount: Number of items to delete (optional)$items: Items to insert (optional)
Returns: ['deleted' => JsArray, 'array' => JsArray]
Example:
$array = JsArray::from([1, 2, 3, 4, 5]);
$result = $array->splice(2, 2, [10, 20]);
// $result['deleted'] = [3, 4]
// $result['array'] = [1, 2, 10, 20, 5]Reverse the array order.
public function reverse(): selfExample:
$array = JsArray::from([1, 2, 3]);
$result = $array->reverse();
// Returns [3, 2, 1]Sort the array.
public function sort(?callable $callback = null): selfParameters:
$callback: Optional comparator functionfn($a, $b) => int
Example:
$array = JsArray::from([3, 1, 2]);
$result = $array->sort();
// Returns [1, 2, 3]
// With comparator
$result = $array->sort(fn($a, $b) => $b <=> $a);
// Returns [3, 2, 1]Concatenate multiple arrays.
public function concat(self ...$arrays): selfExample:
$array1 = JsArray::from([1, 2]);
$array2 = JsArray::from([3, 4]);
$result = $array1->concat($array2);
// Returns [1, 2, 3, 4]Return first element matching the test.
public function find(callable $callback): mixedReturns: The matching element or null
Example:
$array = JsArray::from([1, 2, 3, 4, 5]);
$result = $array->find(fn($n) => $n > 3);
// Returns 4Return index of first matching element.
public function findIndex(callable $callback): int|string|nullReturns: Index (numeric arrays), key (associative), or -1/null if not found
Example:
$array = JsArray::from([1, 2, 3, 4, 5]);
$index = $array->findIndex(fn($n) => $n === 3);
// Returns 2Check if array contains a value (strict comparison).
public function includes(mixed $value): boolExample:
$array = JsArray::from([1, 2, 3]);
$result = $array->includes(2); // true
$result = $array->includes(4); // falseFind first index of a value.
public function indexOf(mixed $searchElement, int $fromIndex = 0): intParameters:
$searchElement: Value to find$fromIndex: Start searching from index
Returns: Index or -1 if not found
Example:
$array = JsArray::from([1, 2, 3, 2, 1]);
$index = $array->indexOf(2); // Returns 1
$index = $array->indexOf(2, 2); // Returns 3Find last index of a value.
public function lastIndexOf(mixed $searchElement, ?int $fromIndex = null): intParameters:
$searchElement: Value to find$fromIndex: Start searching from index (searches backwards)
Returns: Index or -1 if not found
Example:
$array = JsArray::from([1, 2, 3, 2, 1]);
$index = $array->lastIndexOf(2); // Returns 3Test if any element passes the test.
public function some(callable $callback): boolExample:
$array = JsArray::from([1, 2, 3, 4, 5]);
$result = $array->some(fn($n) => $n > 4); // true
$result = $array->some(fn($n) => $n > 10); // falseTest if all elements pass the test.
public function every(callable $callback): boolExample:
$array = JsArray::from([2, 4, 6, 8]);
$result = $array->every(fn($n) => $n % 2 === 0); // true
$result = $array->every(fn($n) => $n > 5); // falseGet the first element.
public function first(): mixedReturns: First element or null if empty
Example:
$array = JsArray::from([1, 2, 3]);
$result = $array->first(); // Returns 1Get the last element.
public function last(): mixedReturns: Last element or null if empty
Example:
$array = JsArray::from([1, 2, 3]);
$result = $array->last(); // Returns 3Get element at index (supports negative indexing).
public function at(int $index): mixedParameters:
$index: Positive or negative index
Returns: Element at index or null if out of bounds
Example:
$array = JsArray::from([1, 2, 3]);
$result = $array->at(0); // Returns 1
$result = $array->at(-1); // Returns 3
$result = $array->at(5); // Returns nullGet all array keys.
public function keys(): selfReturns: JsArray of keys (always immutable)
Example:
$array = JsArray::from(['a' => 1, 'b' => 2]);
$keys = $array->keys(); // Returns ['a', 'b']Get all array values (re-indexed).
public function values(): selfReturns: JsArray of values (always immutable)
Example:
$array = JsArray::from(['a' => 1, 'b' => 2]);
$values = $array->values(); // Returns [1, 2]Add elements to the end.
public function push(mixed ...$values): selfExample:
$array = JsArray::from([1, 2]);
$result = $array->push(3, 4);
// Returns [1, 2, 3, 4]Remove and return the last element.
public function pop(): arrayReturns: ['array' => JsArray, 'value' => mixed]
Example:
$array = JsArray::from([1, 2, 3]);
$result = $array->pop();
// $result['value'] = 3
// $result['array'] = [1, 2]Add elements to the beginning.
public function unshift(mixed ...$values): selfExample:
$array = JsArray::from([3, 4]);
$result = $array->unshift(1, 2);
// Returns [1, 2, 3, 4]Remove and return the first element.
public function shift(): arrayReturns: ['array' => JsArray, 'value' => mixed]
Example:
$array = JsArray::from([1, 2, 3]);
$result = $array->shift();
// $result['value'] = 1
// $result['array'] = [2, 3]Join elements into a string.
public function join(string $separator = ','): stringExample:
$array = JsArray::from([1, 2, 3]);
$result = $array->join(', '); // Returns "1, 2, 3"Execute a function for each element.
public function forEach(callable $callback): voidExample:
$array = JsArray::from([1, 2, 3]);
$array->forEach(fn($value) => echo $value);
// Outputs: 123Convert to native PHP array.
public function toArray(): arrayExample:
$array = JsArray::from([1, 2, 3]);
$result = $array->toArray(); // Returns [1, 2, 3]Convert to immutable mode (creates copy).
public function toImmutable(): selfExample:
$array = JsArray::mutable([1, 2, 3]);
$immutable = $array->toImmutable();Convert to mutable mode (modifies in place).
public function toMutable(): selfExample:
$array = JsArray::from([1, 2, 3]);
$array->toMutable();
// Now $array is mutableCreate a mutable copy.
public function getMutableCopy(): selfExample:
$original = JsArray::from([1, 2, 3]);
$mutable = $original->getMutableCopy();
// $mutable is mutable, $original is unchangedCreate an immutable copy.
public function getImmutableCopy(): selfExample:
$original = JsArray::mutable([1, 2, 3]);
$immutable = $original->getImmutableCopy();
// $immutable is immutable, $original is unchanged- PATTERNS.md - Common patterns and recipes
- EXAMPLES.md - Real-world usage examples
- PERFORMANCE.md - Performance analysis and tips
- MUTABILITY.md - Deep dive into mutable/immutable modes