Template Engine language reference > Arrays > Accessing array elements

Accessing array elements
You use the array index to reference elements in an array.
Click to enlarge
For example, if you have a variable $a that refers to the array [10, 20, 30] you use $a[1] to reference the element value 20. If you try to access an element that does not exist, i.e. by using an index outside the range of the array, an error is generated.
Looping over array elements
You can also access elements in an array by using the #foreach directive to loop over the array elements. For example:
##Example 1
#set($array=['A', 'B', 'C'])
#foreach($i in $array)
$i
#end
 
##Example 2
#foreach($i in ['A', 'B', 'C'])
$i
#end
Accessing elements in two level arrays
To reference an element in an array in an array you must add the index of the second array to the reference. For example, if you have the variable $b that refers to the array [1, [4, 5], 3] you use $b[1][0] to reference the element value 4.
Click to enlarge
Accessing elements in multilevel arrays
If the array contains several levels of arrays you must add the indexes of all levels to the reference. For example, if you have the variable $c that refers to the array [1, [4, [6, 8]], 3] you use $c[1][1][0] to reference the element value 6.
Click to enlarge
Assigning a value to an array element
To assign a value to an array element you use the #set directive. For example:
#set($array1[0]=20)
#set($array2[1][0]='Gold')
Using a variable as index
You can use a variable as index in order to access elements in an array. For example:
$array[$index]
The variable $index must return an integer, otherwise an error is generated.
OpenText StreamServe 5.6.2 Updated: 2018-01-26