<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>article Foreach-loops in Legacy - Engage Bot</title>
    <link>https://community.sinch.com/t5/Legacy-Engage-Bot/Foreach-loops/ta-p/19204</link>
    <description>&lt;DIV class="lia-message-template-content-zone"&gt;
&lt;P&gt;Foreach-construct makes it possible to iterate over variables of type collection and JSON arrays in API-responses.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The basic syntax looks as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;foreach ($iterable, {number_of_iterations})
statement
endforeach&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first parameter specifies the variable to be looped over, the second defines the number of iterations (numeric, optional).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H5&gt;&lt;STRONG&gt;&lt;FONT size="5"&gt;Foreach with collections&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/H5&gt;
&lt;P data-unlink="true"&gt;In our example the user is asked to pick his favorite colors from the list of predefined ones in the variable $color$. The user's choice is then saved into a variable $color$ of&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;datatype&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;collection. The bot confirms user’s choice and returns colors saved in $color$ (max 3). This is achieved by iterating over a variable $color$ (max. 3 iterations) and printing the $value$ of the current color element preceded by its $index$.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="4.png" style="width: 999px;"&gt;&lt;img src="https://community.sinch.com/t5/image/serverpage/image-id/7045i705B2060050AF1E5/image-size/large?v=v2&amp;amp;px=999" role="button" title="4.png" alt="4.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is how it looks in WhatsApp:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="5.png" style="width: 572px;"&gt;&lt;img src="https://community.sinch.com/t5/image/serverpage/image-id/7046i5DCA32649B8B5A15/image-size/large?v=v2&amp;amp;px=999" role="button" title="5.png" alt="5.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H5&gt;&lt;STRONG&gt;&lt;FONT size="5"&gt;Foreach with JSON Arrays&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/H5&gt;
&lt;P&gt;Similarly you an iterate over JSON arrays in API responses. In our example the user receives a weather forecast for the next 3 days, fetched with an API call from&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://openweathermap.org/" target="_blank" rel="noopener"&gt;openweathermap.org&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;The JSON Response contains an array with daily weather information for the upcoming 7 days:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="6.png" style="width: 448px;"&gt;&lt;img src="https://community.sinch.com/t5/image/serverpage/image-id/7047i2A882E9C82C1FBA9/image-size/large?v=v2&amp;amp;px=999" role="button" title="6.png" alt="6.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The complete API response is saved into a variable $api$. The array with daily information is accessed by using a JSON-Path in the variable ($api.daily$) and is used as the first parameter to be iterated over in the foreach-loop. Within the loop you can directly access respective response attributes&amp;nbsp; (in our example: $temp.day$, $weather.0.description$).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="7.png" style="width: 999px;"&gt;&lt;img src="https://community.sinch.com/t5/image/serverpage/image-id/7048i93F07AF145727065/image-size/large?v=v2&amp;amp;px=999" role="button" title="7.png" alt="7.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is how the dialogue looks in WhatsApp:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="9.png" style="width: 583px;"&gt;&lt;img src="https://community.sinch.com/t5/image/serverpage/image-id/7049i992F4C3819D10AB8/image-size/large?v=v2&amp;amp;px=999" role="button" title="9.png" alt="9.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;/DIV&gt;</description>
    <pubDate>Thu, 19 Feb 2026 15:06:47 GMT</pubDate>
    <dc:creator>VishalSandhu</dc:creator>
    <dc:date>2026-02-19T15:06:47Z</dc:date>
    <item>
      <title>Foreach-loops</title>
      <link>https://community.sinch.com/t5/Legacy-Engage-Bot/Foreach-loops/ta-p/19204</link>
      <description>&lt;DIV class="lia-message-template-content-zone"&gt;
&lt;P&gt;Foreach-construct makes it possible to iterate over variables of type collection and JSON arrays in API-responses.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The basic syntax looks as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;foreach ($iterable, {number_of_iterations})
statement
endforeach&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first parameter specifies the variable to be looped over, the second defines the number of iterations (numeric, optional).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H5&gt;&lt;STRONG&gt;&lt;FONT size="5"&gt;Foreach with collections&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/H5&gt;
&lt;P data-unlink="true"&gt;In our example the user is asked to pick his favorite colors from the list of predefined ones in the variable $color$. The user's choice is then saved into a variable $color$ of&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;datatype&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;collection. The bot confirms user’s choice and returns colors saved in $color$ (max 3). This is achieved by iterating over a variable $color$ (max. 3 iterations) and printing the $value$ of the current color element preceded by its $index$.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="4.png" style="width: 999px;"&gt;&lt;img src="https://community.sinch.com/t5/image/serverpage/image-id/7045i705B2060050AF1E5/image-size/large?v=v2&amp;amp;px=999" role="button" title="4.png" alt="4.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is how it looks in WhatsApp:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="5.png" style="width: 572px;"&gt;&lt;img src="https://community.sinch.com/t5/image/serverpage/image-id/7046i5DCA32649B8B5A15/image-size/large?v=v2&amp;amp;px=999" role="button" title="5.png" alt="5.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H5&gt;&lt;STRONG&gt;&lt;FONT size="5"&gt;Foreach with JSON Arrays&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/H5&gt;
&lt;P&gt;Similarly you an iterate over JSON arrays in API responses. In our example the user receives a weather forecast for the next 3 days, fetched with an API call from&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://openweathermap.org/" target="_blank" rel="noopener"&gt;openweathermap.org&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;The JSON Response contains an array with daily weather information for the upcoming 7 days:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="6.png" style="width: 448px;"&gt;&lt;img src="https://community.sinch.com/t5/image/serverpage/image-id/7047i2A882E9C82C1FBA9/image-size/large?v=v2&amp;amp;px=999" role="button" title="6.png" alt="6.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The complete API response is saved into a variable $api$. The array with daily information is accessed by using a JSON-Path in the variable ($api.daily$) and is used as the first parameter to be iterated over in the foreach-loop. Within the loop you can directly access respective response attributes&amp;nbsp; (in our example: $temp.day$, $weather.0.description$).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="7.png" style="width: 999px;"&gt;&lt;img src="https://community.sinch.com/t5/image/serverpage/image-id/7048i93F07AF145727065/image-size/large?v=v2&amp;amp;px=999" role="button" title="7.png" alt="7.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is how the dialogue looks in WhatsApp:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="9.png" style="width: 583px;"&gt;&lt;img src="https://community.sinch.com/t5/image/serverpage/image-id/7049i992F4C3819D10AB8/image-size/large?v=v2&amp;amp;px=999" role="button" title="9.png" alt="9.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;/DIV&gt;</description>
      <pubDate>Thu, 19 Feb 2026 15:06:47 GMT</pubDate>
      <guid>https://community.sinch.com/t5/Legacy-Engage-Bot/Foreach-loops/ta-p/19204</guid>
      <dc:creator>VishalSandhu</dc:creator>
      <dc:date>2026-02-19T15:06:47Z</dc:date>
    </item>
  </channel>
</rss>

