donglian6625 2011-11-17 17:38
浏览 27
已采纳

如何将两个csvs / tab分隔成一个csv与php?

I have two files one is a tab delimited and one is a csv which is comma seperated and they both get uploaded and I need to combine them into one csv thats sorted...here is my code so far

$txt = glob('files/*.txt*');
$csv = glob('files/*.csv*');
$test = array();
if (($handle = fopen($csv[0], "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $row++;
        for ($c=0; $c < $num; $c++) {
            $test[$c][] = $data[$c];
        }
    }
    fclose($handle);
}

if (($handle = fopen($txt[0], "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
        $num = count($data);
        $row++;
        for ($c=0; $c < $num; $c++) {
          $test[$c][] = $data[$c];
        }
    }
    fclose($handle);
}

the test array has the full array of both but i am running into a few problems first the $test array is like this

[0] => Array
       (
           [0] => Edit
           [1] => y
           [2] => y
           [3] => y
           [4] => y
           [5] => y
           [6] => y
           [7] => y
           [8] => y
           [9] => y
[1] => Array
    (
        [0] => Event
        [1] => Carolina Panthers PSL
        [2] => Florida
        [3] => Carolina Panthers PSL
        [4] => Apple
        [5] => Carolina Panthers PSL
        [6] => Carolina Panthers PSL
        [7] => Carolina Panthers PSL
        [8] => Carolina Panthers PSL
        [9] => Carolina Panthers PSL
        [10] => Carolina Panthers PSL
[2] => Array
     (
         [0] => Venue
         [1] => Bank of America Stadium 
         [2] => Washington Mutual 
         [3] => Bank of America Stadium 
         [4] => Apple Inc
         [5] => Bank of America Stadium 
         [6] => Bank of America Stadium 
         [7] => Bank of America Stadium 
         [8] => Bank of America Stadium 
         [9] => Bank of America Stadium 
         [10] => Bank of America Stadium 
         [11] => Bank of America Stadium 
         [12] => Bank of America Stadium

How do i create a new csv with everything but i need to sort by event name and this format for the test array seems off..any ideas what im doing wrong...

  • 写回答

3条回答 默认 最新

  • dtsps00544 2011-11-17 17:55
    关注

    Assuming the column names are on the first line of your file this should work:

    $txt = glob('files/*.txt*');
    $csv = glob('files/*.csv*');
    $test = array();
    $headers = array();
    if (($handle = fopen($csv[0], "r")) !== FALSE) {
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        if (empty($headers)) {
          for ($c=0; $c < $num; $c++) {
            $headers[$c] = $data[$c];
          }
        }
        else {
          $line = array();
          for ($c = 0; $c < $num; $c++) {
            $line[$headers[$c]] = $data[$c];
          }
          $test[] = $line;
        }
      }
    }
    
    if (($handle = fopen($csv[0], "r")) !== FALSE) {
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $line = array();
        for ($c = 0; $c < $num; $c++) {
          $line[$headers[$c]] = $data[$c];
        }
        $test[] = $line;
      }
    }
    

    That should give you an array like

    [0] => Array
    (
           [Edit] => y
           [Event] => Carolina Panthers PSL
           [Venue] => Bank of America Stadium,
           ...
    [1] => Array
    (
    
           [Edit] => y
           [Event] => Florida
           [Venue] => Washington Mutual ,
    ...
    

    Then you can use usort on the array to sort it by the Event key with a custom callback. See the docs page for that function for loads of examples, or try this:

    function my_sort($a, $b) {
      return strcmp($a['Event'], $b['Event']);
    }
    usort($test, 'my_sort');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示