将 Laravel Collection 项合并到新数组中
Merge Laravel Collection Items into new array
我想合并这 collection 项:
Collection {#1242 ▼
#items: array:2 [▼
0 => "1,2,3"
1 => "4,5,6"
]
}
进入这个新数组:
array:6 [▼
0 => "1"
1 => "2"
2 => "3"
3 => "4"
4 => "5"
5 => "6"
]
有什么简单的合并和重新排列的方法吗?
谢谢。
我希望这可以用于你的情况
/*
$collection=[
0 => "1,2,3"
1 => "4,5,6"
];
* if the above is your collection
*/
$result=[];
$collection->each(function($item)use($result){
array_push($result, explode(',', $item));
});
dd($result)
您可以使用 Laravel Collection method called map
and then just explode
each item inside and flatten
the result, also using all
到 return 数组,如下所示:
$collection = $collection->map(function ($item) {
return explode(',', $item);
})->flatten()->all();
我想合并这 collection 项:
Collection {#1242 ▼
#items: array:2 [▼
0 => "1,2,3"
1 => "4,5,6"
]
}
进入这个新数组:
array:6 [▼
0 => "1"
1 => "2"
2 => "3"
3 => "4"
4 => "5"
5 => "6"
]
有什么简单的合并和重新排列的方法吗? 谢谢。
我希望这可以用于你的情况
/*
$collection=[
0 => "1,2,3"
1 => "4,5,6"
];
* if the above is your collection
*/
$result=[];
$collection->each(function($item)use($result){
array_push($result, explode(',', $item));
});
dd($result)
您可以使用 Laravel Collection method called map
and then just explode
each item inside and flatten
the result, also using all
到 return 数组,如下所示:
$collection = $collection->map(function ($item) {
return explode(',', $item);
})->flatten()->all();