I have a simple product system in my project. I need to get my product option names and product options look like this. "Color" => "Red,Blue,Yellow" "Size" => "S,M,L,XL"
My records in database look like this
Option names Options
Color,Size Red-S
Color,Size Blue-S
Color,Size Yellow-S
Color,Size Red-M
Color,Size Blue-M
Color,Size Yellow-M
Color,Size Red-L
This is my code:
foreach ($data as $product) {
$variations = Products::where('group', $product['sku'])->get();
}
$count = count($variations);
if($count > 0){
$array2 = [];
$ss = [];
foreach($variations as $variants){
$oname = explode(',', $variants['o_name']);
$option = explode('-', $variants['option']);
$array = array_combine($oname, $option);
$array2[] = compact('array');
$x = count($oname);
$xz = $x - 1;
for($i = 0; $i <= $xz; $i++){
$xs = $option[$i];
}
$ss[] = $xs;
}
dd($ss);
Output :
array:12 [▼
0 => "S"
1 => "S"
2 => "S"
3 => "M"
4 => "M"
5 => "M"
6 => "L"
7 => "L"
8 => "L"
9 => "XL"
10 => "XL"
11 => "XL"
]
How can i create array like this
"Color" => "Red,Blue,Yellow"