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.

    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试