列表上方打印组名称

发布于 2025-02-11 07:00:09 字数 1083 浏览 2 评论 0原文

按照以下内容

[
  {
   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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没结果 2025-02-18 07:00:09

解决方案:
“ groupby”数组。

$groups = [
    [
        "id"=>1, 
        "group_name"=>"usa", 
        "state" => "San Franciso"
    ],
    [
        "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"
    ]
];

这是在数组中分组项目的简单方法之一,您应该分组“ group_name”字段。 ( https://wwwww.delftstack.com/ - 阵列

$grouped = array();

foreach ($groups as $element) {
    $grouped[$element['group_name']][] = $element;
}

您应该得到这样的东西:

[
  "usa" => [
    ["id" => 1, "state" => "Texas", "group_name" => "usa"],
    ["id" => 2, "state" => "North Carolina", "group_name" => "usa"],
    ["id" => 3, "state" => "Texas", "group_name" => "usa"],
  ],
  "aus" => [
    ["id" => 4, "state" => "Melbourne", "group_name" => "aus"],
    ["id" => 5, "state" => "Darwin", "group_name" => "aus"]
  ]
]

建议:
在将数组发送到树枝之前,请尝试以这种方式构造该数组:

$groups = [
   'usa' => [
      [ "id" => 1, "state" => "Texas" ],
      [ "id" => 2, "state" => "North Carolina" ],
      [ "id" => 3, "state" => "Texas" ],
   ],
   'aus" => [ 
      [ "id" => 4, "state" => "Darwin" ],
      [ "id" => 5, "state" => "Melbourne" ],
    ]

]

在创建之后,您只需要迭代数组并显示值,而无需在Twig中分组数组。

Solution:
"GroupBy" Array.

$groups = [
    [
        "id"=>1, 
        "group_name"=>"usa", 
        "state" => "San Franciso"
    ],
    [
        "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"
    ]
];

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)

$grouped = array();

foreach ($groups as $element) {
    $grouped[$element['group_name']][] = $element;
}

You should get something like this:

[
  "usa" => [
    ["id" => 1, "state" => "Texas", "group_name" => "usa"],
    ["id" => 2, "state" => "North Carolina", "group_name" => "usa"],
    ["id" => 3, "state" => "Texas", "group_name" => "usa"],
  ],
  "aus" => [
    ["id" => 4, "state" => "Melbourne", "group_name" => "aus"],
    ["id" => 5, "state" => "Darwin", "group_name" => "aus"]
  ]
]

Suggestion:
Before you send array to Twig, try to structure that array in this way:

$groups = [
   'usa' => [
      [ "id" => 1, "state" => "Texas" ],
      [ "id" => 2, "state" => "North Carolina" ],
      [ "id" => 3, "state" => "Texas" ],
   ],
   'aus" => [ 
      [ "id" => 4, "state" => "Darwin" ],
      [ "id" => 5, "state" => "Melbourne" ],
    ]

]

And after that creation, you only need to iterate over the array and display values without GroupingBy array in Twig.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文