Constructing Arrays
Indexed arrays
ZYMBA
$numbers = [1, 2, 3, 4, 5];
$names = ["Alice", "Bob", "Charlie"];
$empty = [];
Associative arrays
Use key: value syntax:
ZYMBA
$person = [name: "Alice", age: 30, city: "NYC"];
$config = [host: "localhost", port: 3306, debug: true];
Nested arrays
ZYMBA
$products = [
[id: 1, name: "Widget", price: 9.99],
[id: 2, name: "Gadget", price: 29.99],
[id: 3, name: "Gizmo", price: 49.99]
];
Array generators
ZYMBA
// Range of numbers
$range = @Array.createRange(1, 5, 1); // [1, 2, 3, 4, 5]
// Filled array
$zeros = @Array.createIndex(5, 0); // [0, 0, 0, 0, 0]
// Combine keys and values
$map = @Array.combine(["a", "b", "c"], [1, 2, 3]);
// [a: 1, b: 2, c: 3]
// Create associative array: all keys share the same value
$flags = @Array.createAssociative(["read", "write", "exec"], true);
// [read: true, write: true, exec: true]
// Repeat array contents
$repeated = @Array.repeat([1, 2], 3); // [1, 2, 1, 2, 1, 2]
// Pad array to a target length
$padded = @Array.pad([1, 2, 3], 6, 0); // [1, 2, 3, 0, 0, 0]