Skip to main content

Arrays and Collections

Arrays are Zymba's primary data structure for ordered collections and key-value mappings. The @Array class provides over 100 methods for manipulation, searching, sorting, and functional programming.

In Zymba, arrays and objects are the same underlying type — typeof [1, 2, 3] returns "object". An array with sequential integer keys is an indexed array; one with string keys is an associative array (object-like). The same @Array class handles both.

Complete @Array method reference

Creation

MethodDescription
createIndex($length, $value)Array of given length filled with value
createRange($from, $to, $step)Numeric range
createAssociative($keys, $value)Map from keys — all sharing the same value
combine($keys, $values)Pair up keys and values into associative array
repeat($values, $count)Repeat array contents N times
pad($arr, $length, $padding)Pad array to target length

Adding/removing

MethodDescription
push($arr, $val)Append to end (mutating)
pop($arr)Remove and return last element (mutating)
unshift($arr, $val)Prepend to beginning (mutating)
shift($arr)Remove and return first element (mutating)
insert($arr, $vals, $offset)Insert values at position (mutating)
remove($arr, ...$arrs)Remove values found in given arrays (mutating)
removeKey($arr, $key)Remove element by key (mutating)
removeKeys($arr, $keys)Remove multiple keys (mutating)
removeValue($arr, $val)Remove first occurrence of value (mutating)
removeValues($arr, $vals)Remove all occurrences of values (mutating)
clear($arr)Remove all elements (mutating)

Searching

MethodDescription
findValue($arr, $fn)First element matching callback
findKey($arr, $fn)Key of first element matching callback
keyOf($arr, $val)Key of first occurrence of value
lastKeyOf($arr, $val)Key of last occurrence of value
hasKey($arr, $key)Whether key exists
hasValue($arr, $val)Whether value exists
hasAllKeys($arr, $keys)Whether all specified keys exist
hasAnyKeys($arr, $keys)Whether any specified key exists
hasAllValues($arr, $vals)Whether all specified values exist
hasAnyValues($arr, $vals)Whether any specified value exists
contains($arr, ...$arrs)Whether all elements of given arrays exist in first

Functional

MethodDescription
map($arr, $fn)Transform each element
filter($arr, $fn)Keep elements matching callback
reduce($arr, $initial, $fn)Accumulate single value
forEach($arr, $fn)Execute callback for each element
every($arr, $fn)Whether all elements match
some($arr, $fn)Whether any element matches

Sorting

MethodDescription
sortAscByValues($arr, ?$key)Sort ascending by values
sortDescByValues($arr, ?$key)Sort descending by values
sortAscByKeys($arr)Sort ascending by keys
sortDescByKeys($arr)Sort descending by keys
sortNaturalAscByValues($arr, ?$key)Natural sort ascending
sortNaturalDescByValues($arr, ?$key)Natural sort descending
sortByValues($arr, $fn)Custom comparison sort
sortByKeys($arr, $fn)Custom key sort
sortNaturalAscByKeys($arr)Natural sort ascending by keys
sortNaturalDescByKeys($arr)Natural sort descending by keys
sortMultidimensional($arr, $callbacks)Multi-criteria sort

Transformation

MethodDescription
reverse($arr)Reverse order
unique($arr, ?$key)Remove duplicates (preserves first occurrence keys)
flip($arr, ?$key)Swap keys and values
slice($arr, $offset, ?$length)Extract portion (non-destructive)
splice($arr, $offset, ?$length, $replacement)Replace portion (mutating)
concat($arr, ...$arrs)Concatenate arrays
merge($arr, ...$arrs)Merge (overwrite duplicate keys)
replace($arr, ...$arrs)Replace matching keys from source arrays
partition($arr, $length)Split into chunks of given size
linearize($arr, ?$key)Flatten nested arrays
reindex($arr, ?$initial)Reset keys to sequential numbers
shuffle($arr)Randomize order
toLowerKeys($arr)Lowercase all keys
toUpperKeys($arr)Uppercase all keys

Set operations

MethodDescription
intersectByValues($arr, ...$arrs)Common elements
intersectByKeys($arr, ...$arrs)Common keys
unionByValues($arr, ...$arrs)All unique elements
unionByKeys($arr, ...$arrs)All unique keys
complementByValues($arr, ...$arrs)Elements in first but not others
complementByKeys($arr, ...$arrs)Keys in first but not others

Information

MethodDescription
count($arr)Number of elements
isEmpty($arr)Whether array is empty
isIndexed($arr)Whether all keys are sequential integers
isMultidimensional($arr)Whether any values are arrays
getUniformType($arr)Type name if all elements have same type, else null
countDistinctValues($arr)Number of unique values
countValue($arr, $val)How many times a value appears
countUsedValues($arr)Frequency map (value → count)

Comparison

MethodDescription
equals($arr1, $arr2)Same keys and values (any order)
equalsInOrder($arr1, $arr2)Same keys and values in same order
hasSameKeys($arr1, $arr2)Same keys (any order)
hasSameKeysInOrder($arr1, $arr2)Same keys in same order
hasSameValues($arr1, $arr2)Same values (any order)

String conversion

MethodDescription
joinValues($arr, $delim, ?$key)Join values into string
joinNonEmptyValues($arr, $delim, ?$key)Join non-empty values
joinKeys($arr, $delim)Join keys into string
toCSV($arr, $delim, ?$enclosed)Convert to CSV string
fromCSV($str, $delim)Parse CSV string to array

Access

MethodDescription
firstValue($arr)First element value
lastValue($arr)Last element value
firstKey($arr)First key
lastKey($arr)Last key
keyAt($arr, $offset)Key at numeric position
valueAt($arr, $key)Value at key
head($arr)First element (like Lisp car)
tail($arr)All except first element (like Lisp cdr)
listKeys($arr)Array of all keys
listValues($arr, ?$key)Array of all values (or column from nested arrays)
randomKey($arr)Random key
randomValue($arr)Random value

See also

  • Construction — creating arrays and generators
  • Accessing — dot/bracket notation, first/last, iteration
  • Modifying — adding/removing, merging, slicing, array information
  • Functional and Sorting — map/filter/reduce, searching, sorting, set operations, joining