$units = collect($items)->map(function ($item) {
return [
'name' => $item['name'],
'color' => $item['color'],
];
});
Nov 5, 2019
6
Level 7
Collection method to achieve the following
I would like to know how to achieve the following with Laravel Collections. Also would like to know what would be the name of this, just to replace this useless thread title. Thank you!
$items = [
[
'name' => 'Apple',
'color' => 'green',
'code' => 'foo',
'quantity' => 2
],
[
'name' => 'Orange',
'color' => 'orange',
'code' => 'bar',
'quantity' => 1
],
[
'name' => 'Lemon',
'color' => 'yellow',
'code' => 'baz',
'quantity' => 3
]
];
$units = [];
foreach($items as $item) {
for ($i=0; $i < $item['quantity']; $i++) {
$units[] = [
'code' => $item['code'] . '-' . ($i + 1),
'name' => $item['name'],
'color' => $item['color'],
];
}
}
/*
[
['code' => 'foo-1', 'name' => 'Apple', 'color' => 'green'],
['code' => 'foo-2', 'name' => 'Apple', 'color' => 'green'],
['code' => 'bar-1', 'name' => 'Orange', 'color' => 'orange'],
['code' => 'baz-1', 'name' => 'Lemon', 'color' => 'yellow'],
['code' => 'baz-2', 'name' => 'Lemon', 'color' => 'yellow'],
['code' => 'baz-3', 'name' => 'Lemon', 'color' => 'yellow']
]
*/
Level 104
You can use the index in the map:
$units = collect($items)->map(function ($item, $index) {
return [
'code' => $item['code'] . '-' $index+1,
'name' => $item['name'],
'color' => $item['color'],
];
});
EDIT just saw the update
collect($items)->flatMap(function ($item) {
return collect()->times($item['quantity'])->map(function ($index) use ($item) {
return [
'code' => $item['code'] . '-' . $index,
'name' => $item['name'],
'color' => $item['color'],
];
})->all();
});
2 likes
Please or to participate in this conversation.