【问题标题】:Parsing data into HTML tables and categories将数据解析为 HTML 表格和类别
【发布时间】:2010-07-09 00:10:41
【问题描述】:

我要导入一个文件,比如 june.txt,其中包含以下数据:

    Sandy,820,384,133,18,408
    Wanda,120,437,128,807,595
    Jane,631,415,142,687,600
    Andrea,179,339,349,594,986
    Wanda,803,191,6,807,322
    Jane,741,975,34,15,832
    Jane,239,714,250,94,497
    Andrea,219,188,411,584,713

然后 PHP 会将其解析为两种不同的方式:

第一种方式是将所有名称与总数捆绑在一起,例如:

    Sandy   820     384     133     18      408
    Total   820     384     133     18      408


    Jane    631     415     142     687     600
    Jane    741     975     34      15      832
    Jane    239     714     250     94      497
    Total   1611    2104    426     796     497


    Andrea  179     339     349     594     986
    Andrea  219     188     411     584     713
    Total   398     527     760     1178    1699

    Wanda   120     437     128     807     595
    Wanda   803     191     6       807     322
    Total   923     628     134     1614    917

第二种方法是把名字加在一起形成一个大列表,比如

    Sandy   820     384     133     18      408
    Jane    1611    2104    426     796     497
    Andrea  398     527     760     1178    1699
    Wanda   923     628     134     1614    917

任何逻辑或建议都会有所帮助,我是 PHP 新手,不知道如何做到这一点。我的计划是最终在 HTML 表格中显示结果并使其可排序,但我可以在以后解决这个问题,除非有人觉得有义务在解析中为我添加诸如此类的内容。

【问题讨论】:

    标签: php regex file parsing


    【解决方案1】:

    我认为explode 函数对您有用。

    就创建这些视图而言,我首先将所有这些数据加载到基于名称的关联数组数组中,然后根据需要进行迭代:

    $datafile = file("filename.txt");
    
    // reads lines into an associative array (key is the name) of arrays
    // where each sub-array is a list of the records for each name
    $arr = array();
    foreach($datafile as $line){
        $temp = explode(',', $line);
        $arr[$temp[0]][] = $temp;
    }
    
    // iterate over each person
    foreach($arr as $person_set){
    
        // create an array to hold the sum of each column 
        // (and the name in the first column)
        $totals = array();
        $totals[0] = $person_set[0][0];
        for($i = 1; $i < length($record); $i++){
            $totals[$i] = 0;
        }
    
        // now iterate over each record for this person
        foreach($person_set as $record){
    
            // print a particular record
            echo implode(' ', $record) . '<br>';
    
            // add each column (1..end) to the totals array
            for($i = 1; $i < length($record); $i++){
                $totals[$i] += $record[$i];
            }
        }
    
        // print out the totals line
        echo implode(' ', $totals) . '<br><br>';
    }
    

    我将把这些数据格式化为一个表格作为练习。

    【讨论】:

    • 这太棒了,我已将数据格式化为表格。看来您实际上已经做到了,因此它将每一列和每一行总计为 1 个数字以进行到达分组,而我只想对列进行总计,如我的示例中所示
    • 我得到了 //add 每列 (1..end) for 循环并将其替换为: $totals[i] += $record[1]; $totals[i+1] += $记录[2]; $totals[i+2] += $记录[3]; $totals[i+3] += $记录[4]; $totals[i+4] += $record[5];... 似乎做我想做的事。我不确定 i(不带 $)是否代表。
    • @Brian,这是一个错字,应该是 $totals[$i] 在任何地方都是 $total[i],看我的编辑。
    【解决方案2】:

    嗯,首先,我想用 PHP 的fgetcsv() 来创建一个类似的文件。 Docs.

    【讨论】:

      【解决方案3】:

      你可以试试这样的脚本:

      <?php
      echo "<table>";
      
      $data = file("june.txt");
      foreach($data as $month) {
          $line = explode(',', $data);
          echo '<tr><td>', implode('</td><td>', $line), '</td></tr>';
      }
      
      echo "</table>";
      

      编辑: 我的错,没有注意到您正在排序/分组/总计。不过,这应该让您走上正确的轨道。关键是使用$line 作为您的信息来源。只需将其编译成一个数组并稍后输出(而不是在循环中)。

      【讨论】:

        猜你喜欢
        • 2017-06-30
        • 2017-03-03
        • 1970-01-01
        • 2021-09-08
        • 1970-01-01
        • 1970-01-01
        • 2016-07-14
        • 2015-06-07
        • 2018-08-31
        相关资源
        最近更新 更多