首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >集合group by and concatenate特定列LARAVEL

集合group by and concatenate特定列LARAVEL
EN

Stack Overflow用户
提问于 2021-07-27 17:45:20
回答 1查看 33关注 0票数 0

我有以下回应:

代码语言:javascript
复制
#items: array:4 [▼
    0 => array:6 [▼
      "taskID" => "5"
      "title" => "Idea for the Project"
      "resource" => "Fac - Architect"
      "start" => "Wed Jul 31 08:00:00 EEST 2019"
      "finish" => "Wed Jul 31 17:00:00 EEST 2019"
      "predecessors" => "-"
    ]
    1 => array:6 [▼
      "taskID" => "5"
      "title" => "Idea for the Project"
      "resource" => "Fac - PM"
      "start" => "Wed Jul 31 08:00:00 EEST 2019"
      "finish" => "Wed Jul 31 17:00:00 EEST 2019"
      "predecessors" => "-"
    ]
    2 => array:6 [▼
      "taskID" => "5"
      "title" => "Idea for the Project"
      "resource" => "King"
      "start" => "Wed Jul 31 08:00:00 EEST 2019"
      "finish" => "Wed Jul 31 17:00:00 EEST 2019"
      "predecessors" => "-"
    ]
    3 => array:6 [▼
      "taskID" => "5"
      "title" => "Idea for the Project"
      "resource" => "Stakeholder"
      "start" => "Wed Jul 31 08:00:00 EEST 2019"
      "finish" => "Wed Jul 31 17:00:00 EEST 2019"
      "predecessors" => "-"
    ]
  ]

我想将这些数组分组到一个数组中,并将资源名称连接起来,如下所示:

代码语言:javascript
复制
#items: array:4 [▼
      "taskID" => "5"
      "title" => "Idea for the Project"
      "resource" => [
          "Fac - Architect",
          "Second Resource",
           ...
        ],
      "start" => "Wed Jul 31 08:00:00 EEST 2019"
      "finish" => "Wed Jul 31 17:00:00 EEST 2019"
      "predecessors" => "-"

  ]

有什么帮助吗?如果可以使用集合或数组函数来完成,而不需要使用许多for循环,那就太好了。我正在使用Laravel 8。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-27 21:43:45

这不是最好的解决方案,但您可以这样做

代码语言:javascript
复制
<?php

$items = collect(YOUR_RESPONSE);
$firstItem = $items->shift();
$firstItem['resource'] = [$firstItem['resource']];

$items->reduce(function ($carry, $item) {
    $carry['resource'][] = $item['resource'];
    return $carry;
}, $firstItem);

然后$items->all()会给你你想要的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68542582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档