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¶
Array with elements¶
Array with elements of different types¶
Multi-dimensional array¶
Zephir supports hashes or dictionaries, similar to PHP:
Hash with string keys¶
Hash with numeric keys¶
Hash with mixed string and numeric keys¶
Updating arrays¶
Arrays are updated using square brackets:
String key¶
Numeric key¶
Multi-dimensional array¶
Appending elements¶
Elements can be appended at the end of the array:
Reading elements from arrays¶
Retrieve array elements using either string or numeric keys: