Arrays

These documentation pages are no longer maintained. Please visit the new documentation site.

Arrays are containers that are capable of holding a collection of any other variable type (including arrays). Arrays may be simple vectors or they can be a hash map (associative array). An element of a vector can be accessed using an index number. The number denotes the position of the desired element (the first element is zero, the second element is one, and so on). An element of an associative array can be accessed using both an index number and an identifier.

Regular arrays can be created with the array operator. Associative arrays can be created with the hash operator. The following example code demonstrates the creation of arrays.

Example 1 - array of numbers

{array( 2, 4, 8, 16, 24, 32, 48 )}

 

Creates an array that contains seven numbers.

The array will consist of the following elements:

 

ID   Element

-------------

 0    2

 1    4

 2    8

 3   16

 4   24

 5   32

 6   48

Example 2 - array of strings

{array( 'This', 'is', 'a', 'test' )}

 

Creates an array of strings.

The array will consist of the following elements:

 

ID   Element

-------------

 0   'This'

 1   'is'

 2   'a'

 3   'test'

Example 3 - associative array

{hash('Red', 1, 'Green', 2, 'Blue', 3)}

 

Creates an assoc. array that consists of the following elements:

ID   Element

-------------

Red   1

Green 2

Blue  3

Accessing array elements

Elements of a simple/vector array can only be accessed using numerical indexes. This method is called "index lookup". Elements of an associative array can be accessed by using the key identifiers. This method is called "identifier lookup". The elements of an associative array may also be accessed using index values.

The access method for arrays and objects is similar. This means that the template code can be fed an array or an object and the code wouldn't know the difference. It is also worth to mention that array elements are often referred to as array attributes. The following text explains the use of various lookup methods for arrays.

Index lookup

Index lookup is carried out by appending a period/dot and an index number to the name of a simple/vector or associative array. Index lookup may also be carried out by appending a matching pair of brackets that encapsulate the desired index value. The following example demonstrates how to access the elements of a simple array using index lookup. Notice the different syntax (dot and brackets).

{* Create an array. *}

{let my_array=array( 'Life', 'is', 'very', 'good!' )}

 

{* Extract the elements of the array. *}

The first element is: {$my_array.0} <br />

The second element is: {$my_array.1} <br />

The third element is: {$my_array[2]} <br />

The fourth element is: {$my_array[3]} <br />

 

{* End of let-section. *}

{/let}

The code above will output the following:

The first element is: Life
The second element is: is
The third element is: very
The fourth element is: good!

Identifier lookup

Identifier lookup is carried out by appending a period/dot and an identifier name to the name of an associative array. Identifier lookup may also be carried out by appending a matching pair of brackets that encapsulate the desired index value. The following example demonstrates how to access the elements of an associative array using the identifier lookup method. Notice the different syntax (use of dot and brackets).

{* Create an associative array. *}

{let my_array=hash( 'first', 'Life',

                   'second', 'is',

                   'third', 'very', 

                   'fourth', 'good!' )}

 

{* Extract the elements of the array. *}

The first element is: {$my_array.first}  <br />

The second element is: {$my_array.second} <br />

The third element is: {$my_array[third]} <br />

The fourth element is: {$my_array[fourth]} <br />

 

{* End of let-section. *}

{/let}

This will output the following:

The first element is: Life
The second element is: is
The third element is: very
The fourth element is: good!

As pointed out earlier, the elements of an associative array may also be accessed using the index method. The following example demonstrates this.

{* Create an associative array. *}

{let my_array=hash( 'first', 'Life',

                   'second', 'is', 

                   'third', 'good!' )}

 

{* Extract the elements of the array. *}

The fourth element is: {$my_array.3} <br />

The third element is: {$my_array.2} <br />

The second element is: {$my_array[1]} <br />

The first element is: {$my_array[0]} <br />

 

{* End of let-section. *}

{/let}

The output will be:

The fourth element is: good!
The third element is: very
The second element is: is
The first element is: Life

Comments

Contents

The template language

Template code
Curly bracket issues
Comments
Variable types
    Numbers
    Strings
    Booleans
    Arrays
    Objects


Created

27/02/2004
3:40:28 pm
by Balazs Halasy

Last updated

09/01/2005
6:25:24 pm
by Rune Langseid

Authors

Balazs Halasy
Rune Langseid



This page is part of the eZ Publish documentation. The documentation is available under the GNU Free Documentation License. All contributions will be released under the terms of this license.