duangang79177 2014-07-09 16:55
浏览 186

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

  • 写回答

1条回答 默认 最新

  • douzhuang1900 2014-07-09 19:42
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题