列表上方打印组名称
按照以下内容
[
{
id: 1,
group_name: 'usa',
state : 'San Francisco'
},
{
id: 2,
group_name: 'usa',
state : 'Texas'
},
{
id: 3,
group_name: 'usa',
state : 'North Carolina'
}, {
id: 4,
group_name: 'aus',
state : 'Darwin'
},
{
id: 5,
group_name: 'aus',
state : 'Melbourne'
},
{
id: 6,
group_name: 'usa',
state : 'Perth'
}
]
我想
Group Name: USA
State: San Francisco
State: Texas
State: North Carolina
Group Name: AUS
State: Darwin
State: Perth
State: Sydney
打印以下内容:我尝试在树枝中关注,但在循环中缺乏逻辑,
{% for place in places %}
<tr>
<td>{{ dump(place.getGroupName()) }}</td>
</tr>
<tr>
<td>{{ place.state}}</td>
</tr>
{% endif %}
我想把这个国家置于状态列表之上。在国家清单中,该国必须是各州以及其他国家的同一国家。
I have following array
[
{
id: 1,
group_name: 'usa',
state : 'San Francisco'
},
{
id: 2,
group_name: 'usa',
state : 'Texas'
},
{
id: 3,
group_name: 'usa',
state : 'North Carolina'
}, {
id: 4,
group_name: 'aus',
state : 'Darwin'
},
{
id: 5,
group_name: 'aus',
state : 'Melbourne'
},
{
id: 6,
group_name: 'usa',
state : 'Perth'
}
]
I want to print as following:
Group Name: USA
State: San Francisco
State: Texas
State: North Carolina
Group Name: AUS
State: Darwin
State: Perth
State: Sydney
I tried following in twig but lacked in logic
{% for place in places %}
<tr>
<td>{{ dump(place.getGroupName()) }}</td>
</tr>
<tr>
<td>{{ place.state}}</td>
</tr>
{% endif %}
In the loop I want to put the country above the list of states. In the list of states the country must be top following by the states and same with the other countries as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案:
“ groupby”数组。
这是在数组中分组项目的简单方法之一,您应该分组“ group_name”字段。 ( https://wwwww.delftstack.com/ - 阵列)
您应该得到这样的东西:
建议:
在将数组发送到树枝之前,请尝试以这种方式构造该数组:
在创建之后,您只需要迭代数组并显示值,而无需在Twig中分组数组。
Solution:
"GroupBy" Array.
This is one of simple ways to GroupBy items in array, you should GroupBy "group_name" field. (https://www.delftstack.com/howto/php/php-group-arrays)
You should get something like this:
Suggestion:
Before you send array to Twig, try to structure that array in this way:
And after that creation, you only need to iterate over the array and display values without GroupingBy array in Twig.