如何在CSV文件中聚合(求和)行并使用PHP打印为表


How to aggregate (sum) rows in a CSV file and print as a table using PHP

我正在尝试使用PHP来解释一个简单的CSV日志文件,并将其打印为一个表。

该文件有三列——一个名称,后面跟着两列,其中一列中有1或0。

例如

Test,1,0
Test,0,1
Test2,1,0
Test3,1,0
Test3,0,1
Test3,1,0
Test3,0,1

目标是将所有相同的行相加,得出以下结果:

Test,1,1
Test2,1,0
Test3,2,2

最后将其打印为HTML表格。

到目前为止,我有一个对前两列求和的有效解决方案,但我不知道如何将第三列包括在内。为了概述整个过程,我在一个网页的开头有一个初始的PHP脚本,它将点击记录到CSV文件中:

<?PHP
if (isset($_GET['s1'])){
$sub1 = urlencode($_GET['s1']);
$fp1 = fopen ('botlog.csv', 'a+' );
fputcsv ( $fp1, array ( $sub1, '1', '0' ), ",", '"' );
fclose ( $fp1 );}
?>

稍后,我有第二个脚本,加载在具有JS延迟的iFrame中,它记录类似的值,但记录在第三列而不是第二列:

<?PHP
if (isset($_GET['sub1'])){
$sub2 = urlencode($_GET['sub1']);
$fp2 = fopen ('botlog.csv', 'a+' );
fputcsv ( $fp2, array ( $sub2, '0', '1' ), ",", '"' );
fclose ( $fp2 );}
?>

然后,我有以下内容:a)为第二列值聚合行(还没有弄清楚如何执行第三列值)并放入数组,b)将其全部转储为表:

<?php
$array = array_map('str_getcsv', file('botlog.csv'));
$inputfile  = 'botlog.csv';
$inputHandle = fopen($inputfile, "r");
$sumArray = array();
while (($dataRow = fgetcsv($inputHandle, 1000, ",")) !== FALSE) {
    $subid = $dataRow[0];
    $extra1 =  $dataRow[2];
    if (!isset($sumArray[$subid])) {
        $sumArray[$subid] = 0;
    }
 $sumArray[$subid] += $extra1;
}
var_dump($sumArray); //test sum works
function build_table($sumArray){
    // start table
    $html = '<table>';
    // header row
    $html .= '<tr>';
    $header=array("SUBID"=>"1","Initial Clicks"=>"2","Secondary Clicks"=>"3", "Percentage"=>"4");
    foreach($header as $key=>$value){
            $html .= '<th>' . $key . '</th>';
        }
    $html .= '</tr>';
    // data rows
    foreach( $sumArray as $key=>$value){
        $html .= '<tr>';
        foreach($value as $key2=>$value2){
            $html .= '<td>' . $value2 . '</td>';
        }
        $html .= '</tr>';
    }
    // finish table and return it
    $html .= '</table>';
    return $html;
}
echo build_table($array);
?>

初始代码的工作原理是生成一个数组,其中列2的值相加。哇!然而,我随后尝试在这个sumArray上使用HTML表打印,但它只显示原始内容(即,不是while函数生成的内容)。

因此,目标:

  1. 修改初始代码块以创建一个$sumArray,它合并所有相同的第1列行,但添加它们的第2列和第3列值。

  2. 把它打印在一个漂亮的表格里,第四列是多余的。

非常感谢您的帮助!

编辑:这是我使用的最后一个工作代码:

<?php
if (file_exists('botlog.csv')) {
$array = array_map('str_getcsv', file('botlog.csv'));
$inputfile  = 'botlog.csv';
$inputHandle = fopen($inputfile, "r");
$sumArray = array();
while (($dataRow = fgetcsv($inputHandle, 1000, ",")) !== FALSE) {
    $subid = $dataRow[0];
    if (!isset($sumArray[$subid])) {
        $sumArray[$subid] = array_fill(0, count($dataRow)-1, 0);
    }
    for ($i = 1; $i < count($dataRow); $i++) {
    $sumArray[$subid][$i-1] += $dataRow[$i];
}}
arsort($sumArray);
$table = $sumArray;
echo '<table>';
echo "<thead><td><span>Subid</span></td><td><span>Initial Clicks</span></td><td><span>Secondary Clicks</span></td><td><span>Percentage</span></td></thead>";
foreach ($table as $subids => $values)
{
    echo "<tr><td>".$subids."'n";
    echo "<td>" . $values['0'] . "</td>";
    echo "<td>" . $values['1'] . "</td>";
    echo "<td>Final column contents</td>";
}   
echo "</table>";
}
else{ echo "Botlog.csv file was not found in current directory";}
?>

$sumArray设为二维数组。第一个维度的键是CSV的第一列,第二个维度是其余列的总和。

while (($dataRow = fgetcsv($inputHandle, 1000, ",")) !== FALSE) {
    $subid = $dataRow[0];
    if (!isset($sumArray[$subid])) {
        $sumArray[$subid] = array_fill(0, count($dataRow)-1, 0);
    }
    for ($i = 1; $i < count($dataRow); $i++) {
    $sumArray[$subid][$i-1] += $dataRow[$i];
}