← All docs

Arrays

Indexed arrays, associative arrays, copy-on-write, and built-in array functions.

Indexed arrays

<?php
$arr = [10, 20, 30];
echo $arr[0];          // 10
echo count($arr);      // 3
$arr[1] = 99;          // modify
$arr[] = 40;           // push

String arrays

<?php
$names = ["Alice", "Bob", "Charlie"];
foreach ($names as $name) {
    echo "Hello, " . $name . "\n";
}

Heterogeneous indexed arrays

Indexed arrays can contain different value types. When element types differ, elephc stores the payloads as boxed mixed values internally.

<?php
$items = [1, "two", true];
$items[] = 3.5;

echo $items[0]; // 1
echo $items[1]; // two

Associative arrays

<?php
$map = ["name" => "Alice", "city" => "Paris"];
echo $map["name"];       // Alice
$map["age"] = "30";      // add new key

Associative arrays use a hash table runtime. If later values do not match the first value type, the checker widens to internal mixed runtime shape.

Keys follow PHP’s array-key normalization for integer and string keys. Integer keys remain integers, numeric strings such as "1" normalize to the integer key 1, and strings with leading zeroes such as "01" remain string keys. This applies to literals, reads and writes, foreach, array_keys(), array_search(), array_key_exists(), array_flip(), JSON object keys, and array union.

<?php
$map = [1 => "one", "2" => "two", "02" => "leading"];

echo $map["1"];  // one
echo $map[2];    // two
echo $map["02"]; // leading

Array union

+ between arrays follows PHP union semantics: keys from the left operand win, and only keys that are missing from the left are copied from the right.

<?php
$left = ["a" => "left", "b" => "keep"];
$right = ["a" => "right", "c" => "new"];
$result = $left + $right;

echo $result["a"]; // left
echo $result["c"]; // new

For indexed arrays, numeric keys are preserved. In elephc’s dense indexed-array representation, this means the left side keeps indexes 0..count($left)-1, and only the right suffix with higher numeric indexes is appended.

<?php
$result = [10, 20] + [99, 88, 77];
echo $result[0]; // 10
echo $result[1]; // 20
echo $result[2]; // 77

Union also works across indexed and associative representations. Indexed positions become integer keys in the shared PHP key space, so an associative key "0" blocks right index 0, while "01" remains a distinct string key.

<?php
$left = ["0" => "left zero", "01" => "leading"];
$right = ["right zero", "right one"];
$result = $left + $right;

echo $result[0];    // left zero
echo $result[1];    // right one
echo $result["01"]; // leading

Copy-on-write semantics

Arrays are shared until modified, matching PHP’s by-value behavior:

<?php
$a = [1, 2];
$b = $a;      // shares storage
$b[0] = 9;    // first write detaches $b
echo $a[0];   // 1
echo $b[0];   // 9

The same applies to function parameters and mutating built-ins (array_push(), sort(), shuffle(), etc.).

Multi-dimensional arrays

<?php
$matrix = [[1, 2], [3, 4]];
echo $matrix[0][1];    // 2

Array destructuring

Array destructuring assigns array elements to writable targets. Both short syntax and list(...) are supported.

<?php
[$first, , $third] = [10, 20, 30];
echo $first; // 10
echo $third; // 30

list($left, $right) = [1, 2];

Patterns can be nested, keyed, and can write to the same target forms as ordinary assignments.

<?php
[[$a, $b], [$c, $d]] = [[1, 2], [3, 4]];

["name" => $name, "role" => $role] = ["role" => "admin", "name" => "Ada"];

$items = [0];
[$items[0], $items[]] = [5, 6];

PHP does not allow keyed and unkeyed entries in the same destructuring pattern, and elephc reports that as a compile-time error.

Built-in array functions

FunctionSignatureDescription
count()count($arr_or_countable): intNumber of elements; on objects implementing Countable, dispatches to count()
array_push()array_push($arr, $val): voidAdd element to end
array_pop()array_pop($arr): mixedRemove and return last element
in_array()in_array($needle, $arr): intSearch for value (0/1)
array_keys()array_keys($arr): arrayReturns the array keys
array_values()array_values($arr): arrayReturns copy of values
array_key_exists()array_key_exists($key, $arr): boolCheck if key exists
array_search()array_search($needle, $arr): int|string|falseSearch for value, returning an integer index for indexed arrays, the first matching associative-array key, or false if not found
array_slice()array_slice($arr, $offset [, $length]): arrayExtract a slice
array_splice()array_splice($arr, $offset [, $length]): arrayRemove a slice in place and return the removed elements
array_chunk()array_chunk($arr, $size): arraySplit into chunks
array_merge()array_merge($arr1, $arr2): arrayMerge two arrays
array_combine()array_combine($keys, $values): arrayCreate array from keys/values
array_fill()array_fill($start, $num, $value): arrayFill with values
array_fill_keys()array_fill_keys($keys, $value): arrayFill with values using keys
array_pad()array_pad($arr, $size, $value): arrayPad to length
range()range($start, $end): arraySequential integers
array_diff()array_diff($arr1, $arr2): arrayValues in $arr1 not in $arr2
array_intersect()array_intersect($arr1, $arr2): arrayValues in both
array_diff_key()array_diff_key($arr1, $arr2): arrayKeys in $arr1 not in $arr2
array_intersect_key()array_intersect_key($arr1, $arr2): arrayKeys in both
array_unique()array_unique($arr): arrayRemove duplicates
array_reverse()array_reverse($arr): arrayReverse order
array_flip()array_flip($arr): arrayExchange keys and values, normalizing integer and numeric-string result keys
array_shift()array_shift($arr): mixedRemove and return first
array_unshift()array_unshift($arr, $value): intPrepend element
array_sum()array_sum($arr): int|floatSum of values
array_product()array_product($arr): int|floatProduct of values
array_column()array_column($arr, $column_key): arrayExtract column from array of assoc rows
sort()sort($arr): voidSort ascending (in-place)
rsort()rsort($arr): voidSort descending
asort()asort($arr): voidSort by value, maintain keys
arsort()arsort($arr): voidSort by value desc, maintain keys
ksort()ksort($arr): voidSort by key ascending
krsort()krsort($arr): voidSort by key descending
natsort()natsort($arr): voidNatural order sort
natcasesort()natcasesort($arr): voidCase-insensitive natural sort
shuffle()shuffle($arr): voidRandomly shuffle (in-place)
array_rand()array_rand($arr): intPick one random key
array_map()array_map($callback, $arr): arrayApply callback to each element
array_filter()array_filter($arr, $callback): arrayFilter where callback is truthy
array_reduce()array_reduce($arr, $callback, $init): intReduce to single value
array_walk()array_walk($arr, $callback): voidCall callback on each element
usort()usort($arr, $callback): voidSort with user comparison
uksort()uksort($arr, $callback): voidSort by key with user comparison
uasort()uasort($arr, $callback): voidSort with user comparison, maintain keys
call_user_func()call_user_func($callback, ...): mixedCall a callback value
call_user_func_array()call_user_func_array($callback, $args): mixedCall with args from array
function_exists()function_exists("name"): boolCheck if function is defined
isset()isset($var, ...$vars): intCheck that every variable or offset is defined and not null

Callback arguments can be string literals, first-class callable values, anonymous functions, arrow functions, or variables holding captured closures.

Not supported by design: compact(), extract() require runtime variable-name tables and are listed in the roadmap’s “Will not implement” section.