<?php
$csv = [
['s1','n1','description 1', 'Helmets'],
['s1','n1','description 1a', 'Helmets'],
['s2','n1','description 2', 'Gloves'],
['s2','n1','description 2a', 'Gloves'],
];
$catMap = [
'Helmets' => 30,
'Gloves' => 20,
];
$result = [];
foreach($csv as $row){
//#1 trimming:
$row = array_map('trim', $row);
$row = [
'SKU' => $row[0],
'Product_Name' => $row[1],
'Description' => $row[2],
'Category_ID' => $row[3],
];
$sku = $row['SKU'];
//#2 cat remapping
assert(isset($catMap[$row['Category_ID']]));
$row['Category_ID'] = $catMap[$row['Category_ID']];
//#3 combining
if(isset($result[$sku])){
$result[$sku]['Description'] .= ' ' . $row['Description'];
}else{
$result[$sku] = $row;
}
};
var_dump($result);
- Output for 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- array(2) {
["s1"]=>
array(4) {
["SKU"]=>
string(2) "s1"
["Product_Name"]=>
string(2) "n1"
["Description"]=>
string(28) "description 1 description 1a"
["Category_ID"]=>
int(30)
}
["s2"]=>
array(4) {
["SKU"]=>
string(2) "s2"
["Product_Name"]=>
string(2) "n1"
["Description"]=>
string(28) "description 2 description 2a"
["Category_ID"]=>
int(20)
}
}
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
array(2) {
["s1"]=>
array(4) {
["SKU"]=>
string(2) "s1"
["Product_Name"]=>
string(2) "n1"
["Description"]=>
string(28) "description 1 description 1a"
["Category_ID"]=>
int(30)
}
["s2"]=>
array(4) {
["SKU"]=>
string(2) "s2"
["Product_Name"]=>
string(2) "n1"
["Description"]=>
string(28) "description 2 description 2a"
["Category_ID"]=>
int(20)
}
}
preferences:
178.87 ms | 403 KiB | 172 Q