Functional Operations, Sorting, and Joining
Searching and filtering
Finding elements
ZYMBA
$numbers = [10, 25, 30, 45, 50];
// Find first matching value
$found = @Array.findValue($numbers, function($v) {
return $v > 20;
});
echo $found; // 25
// Find key of first match
$key = @Array.findKey($numbers, function($v) {
return $v > 40;
});
echo $key; // 3
// Find key of specific value
$idx = @Array.keyOf($numbers, 30); // 2
Filtering
ZYMBA
$numbers = [1, 2, 3, 4, 5, 6, 7, 8];
$evens = @Array.filter($numbers, function($n) {
return $n % 2 == 0;
});
// [2, 4, 6, 8]
Testing conditions
ZYMBA
$scores = [85, 92, 78, 95, 88];
// Are ALL scores above 70?
$allPassing = @Array.every($scores, function($s) {
return $s >= 70;
});
// true
// Is ANY score above 90?
$hasExcellent = @Array.some($scores, function($s) {
return $s > 90;
});
// true
Transforming arrays
map
Apply a function to every element:
ZYMBA
$prices = [10, 20, 30];
$withTax = @Array.map($prices, function($price) {
return $price * 1.08;
});
// [10.8, 21.6, 32.4]
$names = ["alice", "bob", "charlie"];
$upper = @Array.map($names, function($n) {
return @String.toUpper($n);
});
// ["ALICE", "BOB", "CHARLIE"]
reduce
Accumulate a single value from all elements:
ZYMBA
$numbers = [1, 2, 3, 4, 5];
$sum = @Array.reduce($numbers, 0, function($acc, $val) {
return $acc + $val;
});
echo $sum; // 15
// Build a string
$words = ["Hello", "World"];
$sentence = @Array.reduce($words, "", function($acc, $word) {
if (empty $acc) return $word;
return $acc . " " . $word;
});
echo $sentence; // "Hello World"
flip, unique, reverse
ZYMBA
// Swap keys and values
$map = [a: 1, b: 2, c: 3];
$flipped = @Array.flip($map);
// [1: "a", 2: "b", 3: "c"]
// Remove duplicate values
$items = [1, 2, 2, 3, 3, 3, 4];
$distinct = @Array.unique($items);
// [1, 2, 3, 4]
// Reverse order
$items = [1, 2, 3, 4, 5];
$reversed = @Array.reverse($items);
// [5, 4, 3, 2, 1]
Sorting
By values
ZYMBA
$numbers = [3, 1, 4, 1, 5, 9];
$asc = @Array.sortAscByValues($numbers); // [1, 1, 3, 4, 5, 9]
$desc = @Array.sortDescByValues($numbers); // [9, 5, 4, 3, 1, 1]
By keys
ZYMBA
$data = [c: 3, a: 1, b: 2];
$sorted = @Array.sortAscByKeys($data); // [a: 1, b: 2, c: 3]
Natural sort (handles numbers in strings)
ZYMBA
$files = ["file10", "file2", "file1", "file20"];
$sorted = @Array.sortNaturalAscByValues($files);
// ["file1", "file2", "file10", "file20"]
Custom sort
ZYMBA
$products = [
[name: "Widget", price: 30],
[name: "Gadget", price: 10],
[name: "Gizmo", price: 20]
];
$byPrice = @Array.sortByValues($products, function($a, $b) {
return $a.price <=> $b.price;
});
Sort by column (multidimensional)
ZYMBA
$users = [
[name: "Charlie", age: 25],
[name: "Alice", age: 30],
[name: "Bob", age: 20]
];
$byAge = @Array.sortAscByValues($users, "age");
// Sorted by age: Bob(20), Charlie(25), Alice(30)
Set operations
By values
ZYMBA
$a = [1, 2, 3, 4, 5];
$b = [3, 4, 5, 6, 7];
$common = @Array.intersectByValues($a, $b); // [3, 4, 5]
$all = @Array.unionByValues($a, $b); // [1, 2, 3, 4, 5, 6, 7]
$diff = @Array.complementByValues($a, $b); // [1, 2]
By keys
ZYMBA
$a = [name: "Alice", age: 30, city: "NYC"];
$b = [name: "Bob", email: "bob@test.com"];
$common = @Array.intersectByKeys($a, $b);
// [name: "Alice"]
$all = @Array.unionByKeys($a, $b);
// [name: "Alice", age: 30, city: "NYC", email: "bob@test.com"]
$diff = @Array.complementByKeys($a, $b);
// [age: 30, city: "NYC"]
Comparison
ZYMBA
$a = [1, 2, 3];
$b = [1, 2, 3];
$c = [3, 2, 1];
// Structural equality (same keys and values, any order)
echo @Array.equals($a, $b); // true
echo @Array.equals($a, $c); // true (same pairs, different order)
// Order-sensitive equality
echo @Array.equalsInOrder($a, $b); // true
echo @Array.equalsInOrder($a, $c); // false
Key transformation
ZYMBA
$data = [Name: "Alice", AGE: 30, City: "NYC"];
$lower = @Array.toLowerKeys($data);
// [name: "Alice", age: 30, city: "NYC"]
$upper = @Array.toUpperKeys($data);
// [NAME: "Alice", AGE: 30, CITY: "NYC"]
Joining and splitting
Join to string
ZYMBA
$items = ["apple", "banana", "cherry"];
$csv = @Array.joinValues($items, ", ");
// "apple, banana, cherry"
$path = @Array.joinKeys([src: 1, main: 1, index: 1], "/");
// "src/main/index"
// Join only non-empty values
$parts = ["John", "", "Doe"];
$name = @Array.joinNonEmptyValues($parts, " ");
// "John Doe"
Join column from multidimensional array
ZYMBA
$users = [
[name: "Alice", email: "alice@test.com"],
[name: "Bob", email: "bob@test.com"]
];
$names = @Array.joinValues($users, ", ", "name");
// "Alice, Bob"
CSV conversion
ZYMBA
// Array to CSV
$data = [["Name", "Age"], ["Alice", "30"], ["Bob", "25"]];
$csv = @Array.toCSV($data, ",", true);
// CSV to Array
$parsed = @Array.fromCSV($csvString, ",");
Practical patterns
Grouping by key
ZYMBA
$orders = [
[status: "pending", total: 100],
[status: "shipped", total: 200],
[status: "pending", total: 150],
[status: "delivered", total: 300]
];
$grouped = [];
for ($orders as $order) {
$grouped[$order.status] ?= [];
$grouped[$order.status][] = $order;
}
// grouped.pending = [{total:100}, {total:150}], etc.
Building a lookup map
ZYMBA
$users = [
[id: 1, name: "Alice"],
[id: 2, name: "Bob"],
[id: 3, name: "Charlie"]
];
$byId = [];
for ($users as $user) {
$byId[$user.id] = $user;
}
echo $byId[2].name; // "Bob"
Pagination
ZYMBA
$allItems = @Array.createRange(1, 100, 1);
$page = 3;
$perPage = 10;
$offset = ($page - 1) * $perPage;
$pageItems = @Array.slice($allItems, $offset, $perPage);
// Items 21-30