我如何格式化以下 laravel collection
how can I format the following laravel collection
提前致谢!我在 Laravel.
中有以下 collection
$data = collect([
[
'id' => 1,
'filter' => 'brand',
'value' => 'apple'
],
[
'id' => 2,
'filter' => 'color',
'value' => 'red'
],
[
'id' => 3,
'filter' => 'color',
'value' => 'blue'
],
[
'id' => 3,
'filter' => 'storage',
'value' => '8GB'
]
]);
我需要这样的:
[
"brand" => ["apple"],
"color" => ["red", "blue"],
"storage" => ["8GB"]
]
如何像上面那样格式化?
我找到了解决方案。
$grouped = $collection->mapToGroups(function ($item, $key) {
return [$item['filter'] => $item['value']];
});
$grouped->toArray();
提前致谢!我在 Laravel.
中有以下 collection$data = collect([
[
'id' => 1,
'filter' => 'brand',
'value' => 'apple'
],
[
'id' => 2,
'filter' => 'color',
'value' => 'red'
],
[
'id' => 3,
'filter' => 'color',
'value' => 'blue'
],
[
'id' => 3,
'filter' => 'storage',
'value' => '8GB'
]
]);
我需要这样的:
[
"brand" => ["apple"],
"color" => ["red", "blue"],
"storage" => ["8GB"]
]
如何像上面那样格式化?
我找到了解决方案。
$grouped = $collection->mapToGroups(function ($item, $key) {
return [$item['filter'] => $item['value']];
});
$grouped->toArray();