集合组由并连接特定列LARAVEL



我有以下回应:

#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" => "-"
]
]

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

#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。

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

<?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()就会给你想要的。

相关内容

  • 没有找到相关文章

最新更新