Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

KingsleyO's avatar

Display array in twig table

I have this code but kind of confused how to display it in a twig table

$data  = array(
    'product' => $product,
      'qty' => $qty,
        'price' => $price,
          'total' => $total,
          

    ); 

the data above are in array so if i do something like

{% for product in product %}
{{ product }}
{% endfor %}

it displays all the items in product. My problem now is how to display it in twig table since it seems i will be running multiple for loops to get all the items in each array.

something i have tried but didn't work how it should

{% for product in product %}
 <tr>

            <td>{{ product }}</td>

        </tr>
{% endfor %}
       {% for price in price %} 
         <tr>

            <td >{{ price }}</td>

        </tr>
{% endfor %}
0 likes
2 replies
Snapey's avatar

Which is why I suggested changing the structure of your input data so that you had a single array of items instead of arrays of attributes.

as it stands, you are going to have to introduce an iterator index and then use this as a pointer into each array.

eg

<td >{{ price[$i] }}</td>
jlrdw's avatar

Just curious why not move over to blade.

Please or to participate in this conversation.