在 PHP 中我会这样做:
$menu_groups = array();
foreach($items as $item){
$menu_id = $item['properties.menu_id'];
if(!array_key_exists($menu_id, $menu_groups)) $menu_groups[$menu_id] = array();
$menu_groups[$menu_id][] = $item;
}
如何在 Twig 模板中完成类似的事情?
you can do like this :
{% set handledPeople = [] %}
{% for person in people if person not in handledPeople %}
<div class="colour_container">
{% for p in people if p.colour == person.colour and p not in handledPeople %}
<p>{{ p.firstname }} {{ p.surname }} - {{ p.colour }}</p>
{% set handledPeople = handledPeople|merge([p]) %}
{% endfor %}
</div>
{% endfor %}