将两个 collections 合并为 laravel
combine two collections together in laravel
我有一个 $sizes
collection 这样的:
$sizes =
collect([
10 => 'xl',
11 => 'xxl',
12 => 'xxxl'
]);
还有一个 $colors
collection 这样的:
$colors =
collect([
20 => 'red',
21 => 'green',
22 => 'blue'
]);
现在我想创建这样的组合:
$result =
collect([
[10 => 'xl' , 20 => 'red'],
[10 => 'xl' , 21 => 'green'],
[10 => 'xl' , 22 => 'blue'],
[11 => 'xxl' , 20 => 'red'],
[11 => 'xxl' , 21 => 'green'],
[11 => 'xxl' , 22 => 'blue'],
[12 => 'xxxl' , 20 => 'red'],
[12 => 'xxxl' , 21 => 'green'],
[12 => 'xxxl' , 22 => 'blue']
]);
我如何在 laravel 中做到这一点?
这是一个 crossJoin 操作。
文档:https://laravel.com/docs/5.6/collections#method-crossjoin
我有一个 $sizes
collection 这样的:
$sizes =
collect([
10 => 'xl',
11 => 'xxl',
12 => 'xxxl'
]);
还有一个 $colors
collection 这样的:
$colors =
collect([
20 => 'red',
21 => 'green',
22 => 'blue'
]);
现在我想创建这样的组合:
$result =
collect([
[10 => 'xl' , 20 => 'red'],
[10 => 'xl' , 21 => 'green'],
[10 => 'xl' , 22 => 'blue'],
[11 => 'xxl' , 20 => 'red'],
[11 => 'xxl' , 21 => 'green'],
[11 => 'xxl' , 22 => 'blue'],
[12 => 'xxxl' , 20 => 'red'],
[12 => 'xxxl' , 21 => 'green'],
[12 => 'xxxl' , 22 => 'blue']
]);
我如何在 laravel 中做到这一点?
这是一个 crossJoin 操作。
文档:https://laravel.com/docs/5.6/collections#method-crossjoin