我想以排序格式获取数据。这是我的数组
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 47
[user_id] => 2
[title] => SSS
[category_id] => 4
[description] => sss description sss description sss description sss description sss description sss description sss description sss description sss description sss description
[deliverable_status] => pending reviews
[created_at] => 2018-01-17 13:26:19
[updated_at] => 2018-01-17 13:26:19
[category_name] => Designs
[reviewer_user_id] => 3
[email] => def@yopmail.com
)
[1] => stdClass Object
(
[id] => 47
[user_id] => 2
[title] => SSS
[category_id] => 4
[description] => sss description sss description sss description sss description sss description sss description sss description sss description sss description sss description
[deliverable_status] => pending reviews
[created_at] => 2018-01-17 13:26:19
[updated_at] => 2018-01-17 13:26:19
[category_name] => Designs
[reviewer_user_id] => 4
[email] => abc@yopmail.com
)
)
)
这两条记录除了评论者ID和电子邮件之外都有相同的数据。所以我需要单个数组和两个电子邮件。我需要单个数组中的那些。这是我需要的样本数组
[0] => stdClass Object
(
[id] => 47
[user_id] => 2
[title] => SSS
[category_id] => 4
[description] => sss description sss description sss description sss description sss description sss description sss description sss description sss description sss description
[deliverable_status] => pending reviews
[created_at] => 2018-01-17 13:26:19
[updated_at] => 2018-01-17 13:26:19
[category_name] => Designs
[email] => [0] => stdClass Object (
[0] => 'def@yopmail.com',
[1] => 'abc@yopmail.com',
)
)
答案 0 :(得分:1)
使用以下代码:
$result = $collection->unique('id')
->map(function ($item) use ($collection) {
$item->email = $collection->where('id', $item->id)
->pluck('email')
->toArray();
return $item;
})
->first();
结果将是:
stdClass Object
(
[id] => 47
[user_id] => 2
[title] => SSS
[category_id] => 4
[description] => sss description sss description sss description sss description sss description sss description sss description sss description sss description sss description
[deliverable_status] => pending reviews
[created_at] => 2018-01-17 13:26:19
[updated_at] => 2018-01-17 13:26:19
[category_name] => Designs
[email] => Array (
[0] => 'def@yopmail.com',
[1] => 'abc@yopmail.com',
)
)
答案 1 :(得分:0)
您可以尝试:
$sortedarray = array_unique(array_merge($items[0],$items[1]), SORT_REGULAR);
答案 2 :(得分:0)
如果您从数据库获取数据,可以在查询中以选择方法
进行尝试DB::raw('group_concat(colum_name) as names')