<?php
$array = [
[
0 => 'Title',
1 => 'image_url',
3 => 'SKU CODE',
4 => 'TITLE SIZE',
5 => 'DESCRIPTION',
6 => 'BASE SKU',
],
[
0 => 'A witch and her cat live here',
1 => 'https://beautifulhomegifts.com/a-witch-and-her-cat-live-here/',
3 => [
0 => 'BHG-MS-AWAHCLH030720',
1 => 'BHG-MS-AWAHCLH030720-A5',
2 => 'BHG-MS-AWAHCLH030720-A4',
3 => 'BHG-MS-AWAHCLH030720-A3',
],
4 => [
0 => 'A witch and her cat live here',
1 => 'A witch and her cat live here - 150mm x 200mm',
2 => 'A witch and her cat live here - 201mm x 305mm',
3 => 'A witch and her cat live here - 305mm x 400mm',
],
5 => [
0 => 'A witch and her cat live here',
1 => 'A witch and her cat live here',
2 => 'A witch and her cat live here',
3 => 'A witch and her cat live here',
],
6 => [
1 => 'BHG-MS-AWAHCLH030720',
2 => 'BHG-MS-AWAHCLH030720',
3 => 'BHG-MS-AWAHCLH030720',
],
],
2 => [
0 => '',
1 => '',
],
3 => [
0 => '',
1 => '',
],
4 => [
0 => '',
1 => '',
],
5 => [
0 => 'Autism House Rules',
1 => 'https://beautifulhomegifts.com/autism-house-rules/',
3 => [
0 => 'BHG-MS-AHR030720',
1 => 'BHG-MS-AHR030720-A5',
2 => 'BHG-MS-AHR030720-A4',
3 => 'BHG-MS-AHR030720-A3',
],
4 => [
0 => 'Autism House Rules',
1 => 'Autism House Rules - 150mm x 200mm',
2 => 'Autism House Rules - 201mm x 305mm',
3 => 'Autism House Rules - 305mm x 400mm',
],
5 => [
0 => 'Autism House Rules',
1 => 'Autism House Rules',
2 => 'Autism House Rules',
3 => 'Autism House Rules',
],
6 => [
1 => 'BHG-MS-AHR030720',
2 => 'BHG-MS-AHR030720',
3 => 'BHG-MS-AHR030720',
],
]
];
$headers = array_shift($array);
// $fhandle = fopen("new.csv", "a");
// fputcsv($fhandle, $headers);
$demo[] = $headers;
foreach ($array as $row) {
if (empty($row[0])) {
continue;
}
$group = array_splice($row, 0, 2);
foreach ($row[0] as $column => $notUsed) {
/* fputcsv($fhandle, array_merge(
!$column ? $group : ['', ''],
array_column($row, $column)
)); */
$demo[] = array_merge(
!$column ? $group : ['', ''],
array_column($row, $column)
);
}
}
// fclose($fhandle);
var_export($demo);
preferences:
27.49 ms | 408 KiB | 5 Q