Skip to content

Arrays

Array manipulation in Zephir provides a way to leverage PHP arrays. An array in Zephir corresponds to the concept of a hash table in other programming languages.

Declaring Array Variables

Array variables can be declared using the keywords var or array:

var a   = []; // array variable, its type can be changed
array b = []; // array variable, its type cannot be changed across execution

Creating Arrays

Arrays are created by enclosing elements in square brackets:

Empty array
let elements = [];
Array with elements
let elements = [1, 3, 4];
Array with elements of different types
let elements = ["first", 2, true];
Multi-dimensional array
let elements = [[0, 1], [4, 5], [2, 3]];

Zephir supports hashes or dictionaries, similar to PHP:

Hash with string keys
let elements = ["foo": "bar", "bar": "foo"];
Hash with numeric keys
let elements = [4: "bar", 8: "foo"];
Hash with mixed string and numeric keys
let elements = [4: "bar", "foo": 8];

Updating arrays

Arrays are updated using square brackets:

String key
let elements["foo"] = "bar";
Numeric key
let elements[0] = "bar";
Multi-dimensional array
let elements[0]["foo"] = "bar";
let elements["foo"][0] = "bar";

Appending elements

Elements can be appended at the end of the array:

let elements[] = "bar";

Reading elements from arrays

Retrieve array elements using either string or numeric keys:

Using the string key
let foo = elements["foo"];
Using the numeric key
let foo = elements[0];