我有以下回应:
#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。
发布于 2021-07-27 21:43:45
这不是最好的解决方案,但您可以这样做
<?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()会给你你想要的。
https://stackoverflow.com/questions/68542582
复制相似问题