Skip to main content

Modifying Arrays

Adding elements

ZYMBA
$items = [1, 2, 3];

// Append to end
$items[] = 4; // [1, 2, 3, 4]
@Array.push($items, 5); // [1, 2, 3, 4, 5]

// Prepend to beginning
@Array.unshift($items, 0); // [0, 1, 2, 3, 4, 5]

// Set by key
$items["key"] = "value";
$items.newKey = "value";

Removing elements

ZYMBA
$items = [1, 2, 3, 4, 5];

// Remove from end
$last = @Array.pop($items); // $last = 5, array = [1, 2, 3, 4]

// Remove from beginning
$first = @Array.shift($items); // $first = 1, array = [2, 3, 4]

// Remove by key
@Array.removeKey($items, "0");

// Remove by value
@Array.removeValue($items, 3);

// Remove multiple values
@Array.removeValues($items, [2, 4]);

// Delete a property
delete $items.someKey;

Concatenation and merging

ZYMBA
$a = [1, 2, 3];
$b = [4, 5, 6];

// Concat preserves keys from both
$combined = @Array.concat($a, $b); // [1, 2, 3, 4, 5, 6]

// Merge overwrites duplicate keys
$defaults = [color: "red", size: "M"];
$custom = [size: "L", weight: 10];
$merged = @Array.merge($defaults, $custom);
// [color: "red", size: "L", weight: 10]

// Replace matching keys from source arrays
$base = [a: 1, b: 2];
$updates = [b: 99, c: 3];
$result = @Array.replace($base, $updates);
// [a: 1, b: 99, c: 3]

Slicing and splicing

ZYMBA
$items = [10, 20, 30, 40, 50];

// Slice extracts a portion (non-destructive)
$middle = @Array.slice($items, 1, 3); // [20, 30, 40]

// Negative offset counts from end
$last2 = @Array.slice($items, -2); // [40, 50]

// Splice modifies in-place: remove 2 at index 1, insert 99, 88
@Array.splice($items, 1, 2, [99, 88]);

Array information

Size and emptiness

ZYMBA
$items = [1, 2, 3];

echo @Array.count($items); // 3
echo count $items; // 3 (operator form)

echo @Array.isEmpty($items); // false
echo @Array.isEmpty([]); // true

Key and value checks

ZYMBA
$config = [debug: true, level: 3, format: "json"];

@Array.hasKey($config, "debug"); // true
@Array.hasKey($config, "missing"); // false
@Array.hasValue($config, 3); // true
@Array.hasAllKeys($config, ["debug", "level"]); // true
@Array.hasAnyKeys($config, ["missing", "debug"]); // true

Structure inspection

ZYMBA
$indexed = [1, 2, 3];
$assoc = [a: 1, b: 2];
$nested = [[1, 2], [3, 4]];

echo @Array.isIndexed($indexed); // true
echo @Array.isIndexed($assoc); // false
echo @Array.isMultidimensional($nested); // true
echo @Array.getUniformType($indexed); // "int"

Reindexing

ZYMBA
$items = [a: "x", b: "y", c: "z"];
$indexed = @Array.reindex($items, 0);
// [0: "x", 1: "y", 2: "z"]

Partitioning

ZYMBA
$items = [1, 2, 3, 4, 5, 6, 7, 8];
$chunks = @Array.partition($items, 3);
// [[1, 2, 3], [4, 5, 6], [7, 8]]

Shuffling and random selection

ZYMBA
$deck = @Array.createRange(1, 52, 1);
$shuffled = @Array.shuffle($deck);

$colors = ["red", "green", "blue", "yellow"];
$random = @Array.randomValue($colors); // Random element
$randomKey = @Array.randomKey($colors); // Random index

Value counting

ZYMBA
$items = [1, 2, 2, 3, 3, 3];

echo @Array.countDistinctValues($items); // 3

$freq = @Array.countUsedValues($items);
// [1: 1, 2: 2, 3: 3]

echo @Array.countValue($items, 3); // 3