CSV,将多行数据合并为单行并放入同一列
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?

UPDATE

Below is the screen shot of what is needed:- enter image description here

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

1个回答

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问