Iterate

Loop through a list of Objects.

iterate is similar to foreach but loops through each array/list member, setting a variable to the value of the array element rather than the key (as foreach does). It greatly simplifies the syntax required to iterate through a list of objects.

Braces are mandatory. break and continue may be used – break to terminate the loop (as previously described) and continue to restart the loop from the beginning with the next value.

There are two syntaxes for iterate – both operate in the same way, use whichever you prefer:


iterate (name: expression) {
 // loop-body
 }

iterate name in expression {
 // loop-body
 }

NOTE: Unlike foreach, iterate only operates with Arrays/Lists. Because it sets the variable to the content of each array element, the key value is not available in the body of the loop.

Example: Here is an example of an Array iterator – this iterates over all the values in the specified array (i.e.: the subscripts). e.g.

set arr = { ';a'; =\> ';1';, ';b'; =\> ';2';, ';c'; =\> ';3'; };
iterate(k: $arr) {

// $k will iterate through the values "1", "2; and "3";
echo "$k"; // output will be 1, 2, 3
 }

Here is an example of iterating through a list of Objects. Get a list of UserGroups to which the invoking user belongs:

set me=me();

echo "hello ${me.name}";

iterate(g: ${me.groups}) {

echo ${g.name};

}

### Result:

hello admin

EVERYONE

Developers

Administrators
Last modified June 17, 2020: cleanup (e989440)