twigHow to loop through an array in Twig and PHP?
Looping through an array in Twig and PHP is a common task. The following example code block shows how to loop through an array in Twig and PHP:
{% for item in array %}
{{ item }}
{% endfor %}
The output of the above code would be the items in the array, each on a new line.
Code explanation
{% for item in array %}
: This is the start of the loop. It declares a variableitem
which will be used to refer to each item in the array.{{ item }}
: This is the code that will be executed for each item in the array. In this case, it will print the item.{% endfor %}
: This is the end of the loop.
Helpful links
More of Twig
- How to format a date using PHP and Twig?
- How to use Twig in PHP to get the current year?
- How to check if a string contains a substring in PHP Twig?
- How to embed YouTube videos in Twig with PHP?
- How to handle whitespace in Twig with PHP 7.4?
- How to get the user agent in PHP Twig?
- How to set a session variable in PHP Twig?
- How to use a switch case in PHP Twig?
- How to prevent Server-Side Template Injection (SSTI) in PHP Twig?
- How to require a PHP file in Twig?
See more codes...