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:- enter image description here

duandongjin5647 这里的人们通常希望askers能够展示他们尝试过的东西,但无论如何,这是一个有趣的问题。
大约 6 年之前 回复
douzhi7070 -是的,有些产品具有不同数量的图像和类别,这些产品并不是全部相同。
大约 6 年之前 回复
dsbfbz75185 我添加了截图,希望它可以帮助理解这个问题。
大约 6 年之前 回复
douzhuan0309 我不明白:您希望在csv文件中合并多个行的单行组?
大约 6 年之前 回复
dpyic24480 好吧,我必须在合并行后更新CSV文件。目前与mysql没有任何关系
大约 6 年之前 回复
dongpang2483 你的意思是通过GROUP_CONCAT函数?
大约 6 年之前 回复


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:


function setup_row($groups, $row){
  $res = $row;
  array_map(function($v) use(&$res,$row){
      $res[$v] = array($row[$v]);
  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
  array_map(function($v) use ($row, &$merge) {
      if (isset($row[$v]) && $row[$v] != "")
        $merge[$v] = $row[$v]; // coalesce columns

function implode_row($groups, $merge){
  array_map(function($v) use (&$merge) {
      $merge[$v] = implode(',', $merge[$v]);
  return $merge;

function csv_group_by($groups, $coalesces, $group_by, $infile, $outfile){
  $current_group = "";
  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,',');

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.

Csdn user default icon