Based on them, I want to generate an HTML table in which the first column will be the first element of the collection1 array, and the second column will contain the first element of the collection2 array.
So I have the html.twig code:
{% set collection1 = page.collection %}
{% set collection2 = page.collection('nabor2') %}
{% set max_length = max(collection1|length, collection2|length) %}
<table class="table">
{% for i in 0..max_length-1 %}
<tr>
<td>{{collection1[i].title}}</td>
<td>{{collection2[i].title}}</td>
</tr>
{% endfor %}
</table>
However, if I make a classic loop to display the content, it obviously works:
{% for item in collection1 %}
{{debug(item.title)}}
{% endfor %}
and as a result I get a list of titles.
I understand that a collection object is not an array. However, I don’t know how to iteratively access each element, considering that in the view I have to match the first element of collection collection1 with the first element of collection2, etc.
Do you have title if you use toExtendedArray?
In any case, I see keys are as paths in the converted array, so you’d need to convert it to indexed array. In PHP there’s array_values function, but I don’t see this in Twig. You could try .toExtendedArray.array_values, but might not work. In such case I’d probably do a bit differently - do two loops to construct your arrays with titles from your collections and then a third loop like you did initially