media_gallery_image category sku created_at price
/c/u/cutting_shafts.jpg Specials 01mb 3/19/2013 22:59 1487
/s/h/shred_bin_3.jpg Cross Cut
/e/c/ecc_switch_3.jpg Destroyit
/2/6/2604ccm.jpg Commercial
Paper Phoenix
These are the details of one product in CSV file. For this product, its showing me a sku, creation date, price and multiple categories & gallery images.
Now the problem is that its showing first media gallery image, first category, sku as 01mb, date as 3/19/2013 22:59, price as 1487 in one row,
then in 2nd row it shows 2nd gallery image, 2nd category, sku as blank, date as blank, price as blank
then in 3rd row(same as 2nd row) and so on.
I have 100s of products this way, they all have multiple images, categories but not showing in the same product data row as above. I want to merge all the media gallery images in same row and in the same column separated with comma(,). For example "/c/u/cutting_shafts.jpg, /s/h/shred_bin_3.jpg, /e/c/ecc_switch_3.jpg, /2/6/2604ccm.jpg"
and same for the categories column.
Is it possible to do this by editing and updating CSV through php?
Below is the screen shot of what is needed:-
The following performs something similar to a group by and coalesce on columns specified as parameters. It may be used to do what you want:
<?php
function setup_row($groups, $row){
$res = $row;
array_map(function($v) use(&$res,$row){
$res[$v] = array($row[$v]);
},$groups);
return $res;
}
function concat_row($groups, $coalesces, $row, &$merge){
array_map(function($v) use ($row, &$merge) {
if (isset($row[$v]) && $row[$v] != "")
$merge[$v][] = $row[$v]; // add to group concat columns
},$groups);
array_map(function($v) use ($row, &$merge) {
if (isset($row[$v]) && $row[$v] != "")
$merge[$v] = $row[$v]; // coalesce columns
},$coalesces);
}
function implode_row($groups, $merge){
array_map(function($v) use (&$merge) {
$merge[$v] = implode(',', $merge[$v]);
},$groups);
return $merge;
}
function csv_group_by($groups, $coalesces, $group_by, $infile, $outfile){
$current_group = "";
$result;
if (($hin = fopen($infile, "r")) !== FALSE &&
($hout = fopen($outfile, "w")) !== FALSE) {
while (($row = fgetcsv($hin, 0, ",")) !== FALSE) {
if ($row[$group_by] != $current_group){
if (isset($merge)){
$result = implode_row($groups, $merge);
fputcsv($hout, $result,',');
}
$merge = setup_row($groups, $row);
} else {
concat_row($groups, $coalesces, $row, $merge);
}
}
if (isset($merge)){
$result = implode_row($groups, $merge);
fputcsv($hout, $result,',');
}
fclose($hin);
fclose($hout);
}
}
csv_group_by(array(1,2), array(), array(5),”infile”,"outfile");
?>
Replace outfile
and outfile
by the file names you want in the last line.
Or replace the end of the script, after the functions with
If (count($argv) == 3)
csv_group_by(array(1,2), array(), array(5),$argv[1],$argv[2]);
?>
And call the script at the command line as follow:
$ php script.php <infile> <outfile>
Where <infile>
and <outfile>
have the expected meanings.
Oh, and the script expects csv files to be comma separated, you'll have to edit the code to change that behaviour.