PHP根据值列对文本文件上的行/行进行计数

时间:2018-09-19 10:24:46

标签: php

继续我以前的question

我有一个称为拒绝的文本日志文件。您可以看到它here

如您所见,选项卡上有四个步骤:

1. Battery level
2. Piezo sound level
3. Left D3  (Ch 3) light intensity
4. Right D2  (Ch 1) light intensity

enter image description here

现在,我想对每个带有条件的行进行计数:

Which column(Steps) value is filled then count it.
Example: on row 1, We can see the value 0 (any value) is on step Piezo sound level. Then count it.

所以最终我可以知道有多少数量的拒绝过程。

Battery level = x quantity
Piezo sound level = x quantity
Left D3  (Ch 3) light intensity = x quantity
Right D2  (Ch 1) light intensity = x quantity

PHP代码:

$fromDateTime = new DateTime('Wed, Sep 19  2018 08:00:00');
$toDateTime = new DateTime('Wed, Sep 19  2018 19:59:00');
$file = file_get_contents('reject.txt');
$lines = explode("\n",$file);

// counter
$rowsintimespan = 0;

// Do Line-By-Line starting by Line 16 (Array Index 15)
for($i = 15; $i < count($lines); $i++) {
// if the file is "Tue, Sep 18<tab>2018<tab>23:59:53<tab>"
$dateobj = DateTime::createFromFormat("???,?M?d??Y?H:i:s+", $lines[$i]);

// check if date is in your Timespan
if($dateobj < $toDateTime && $dateobj > $fromDateTime) {
        $rowsintimespan++; // count if in timespan
    }
}

// Debug-Output
echo $rowsintimespan;

更新

我需要读取最后一列的值,例如:如果该行的值在左D3列上,则对其进行计数。如果row的值在Piezo列上,则对其进行计数。

enter image description here

2 个答案:

答案 0 :(得分:1)

如果您可以将列记为键,则可以按照您的描述进行操作:

$fromDateTime = new DateTime('Wed, Sep 19  2018 08:00:00');
$toDateTime = new DateTime('Wed, Sep 19  2018 19:59:00');
$file = file_get_contents('Reject.txt');
$lines = explode("\n", $file);

// counter
$rowsintimespan = 0;
// keys should correspond to columns
$keys = [
    'date',
    'time',
    'battery',
    'piezo',
    'leftD3',
    'rightD2'
];

$values = array_fill(0, count($keys), 0);
$values = array_combine($keys, $values);

// Do Line-By-Line starting by Line 16 (Array Index 15)
for ($i = 11; $i < count($lines); $i++) {
    // if the file is "Tue, Sep 18<tab>2018<tab>23:59:53<tab>"
    $dateobj = DateTime::createFromFormat("???,?M?d??Y?H:i:s+", $lines[$i]);

    // check if date is in your Timespan
    if ($dateobj < $toDateTime && $dateobj > $fromDateTime) {
        $rowsintimespan++; // count if in timespan

        // get line elements
        $lineContent = explode("\t", $lines[$i]);

        // loop through line elements and count them
        $x = 0;
        for ($j = 0; $j < count($keys); $j++) {
            if (!isset($lineContent[$j])) {
                continue;
            }

            // remember position of last not empty column
            if (trim($lineContent[$j]) != '') {
                $x = $j;
            }
        }

        if ($x > 0) {
            $values[$keys[$x]]++;
        }
    }
}

// Debug-Output
echo $rowsintimespan;

// Output every column
echo '<pre>';
print_r($values);

这将打印出来:

Array
(
    [date] => 0
    [time] => 0
    [battery] => 4
    [piezo] => 31
    [leftD3] => 17
    [rightD2] => 1
)

答案 1 :(得分:0)

只需按标签将行分开,然后检查!empty();

//Set Time-Span
$fromDateTime = new DateTime('Wed, Sep 19  2018 00:00:00');
$toDateTime = new DateTime('Wed, Sep 19  2018 17:00:00');

// Load File
$file = file_get_contents('Reject.txt');

// Split by lines
$lines = explode("\n",$file);

// counter
$rowsintimespan = 0;
$rowswithbattery = 0;

// Do Line-By-Line starting by Line 16 (Array Index 15)
for($i = 15; $i < count($lines); $i++) {
    // if the file is "Tue,<space>Sep<space>18<space><space>2018<tab>23:59:53<tab>"
    $dateobj = DateTime::createFromFormat("???, M  d?Y?H:i:s+", $lines[$i]);

    // check if date is in your Timespan
    if($dateobj < $toDateTime && $dateobj > $fromDateTime) {
        $rowsintimespan++; // count if in timespan
        $cols = explode("\t",$lines[$i]);
        // 0 = Date, 1 = Time, 2 = Battery Level, 3 = Piezo, 4 = left, 5 = Right
        // Count Battery-Values
        if (!empty($cols[2])) {
            $rowswithbattery++;
        }
    }
}

// Debug-Output
echo 'In Timespan: '.$rowsintimespan."\n";
echo 'Rows With Battery: '.$rowswithbattery

产生:

In Timespan: 84
Rows With Battery: 84

包含所有列的完整示例:https://ideone.com/VAQH7h

您还可以遍历每个标头并创建一个整齐的数组,如我在https://ideone.com/YUU1jP中所示

// Split by lines
$lines = explode("\n", $file);
$header = explode("\t", $lines[9]);
// counter
$rowsintimespan = 0;
$counter = Array();

// Create an entry for every header and trim it to lose additional whitespaces
foreach($header as $index => $head){
    $counter[rtrim($head)] = 0;
}

// Do Line-By-Line starting by Line 16 (Array Index 15)
for($i = 11; $i < count($lines); $i++) {
    // the file is "Tue,<space>Sep<space>18<space><space>2018<tab>23:59:53<tab>"
    $dateobj = DateTime::createFromFormat("???, M d  Y?H:i:s+", $lines[$i]);
    // check if date is in your Timespan
    if($dateobj < $toDateTime && $dateobj > $fromDateTime) {
        $rowsintimespan++; // count if in timespan
        $cols = explode("\t",$lines[$i]);
        // 0 = Date, 1 = Time, 2 = Battery Level, 3 = Piezo, 4 = left, 5 = Right
        // Count Battery-Values
        foreach($header as $index => $head){
            // For every header check if col is empty
            $counter[rtrim($head)] += empty($cols[$index]) ? 0 : 1;
        }
    }
}

// Debug-Output
echo 'In Timespan: '.$rowsintimespan."\n";
var_dump($counter);

更新以匹配更新的问题:

然后只需做一些简单的数学运算即可。

理论:如果您有80个带电池的固定列和20个带固定压电的列,则可以从80个电池计数器中减去20个压电计数器,然后知道多少个电池列和最后一个压电列

因此:

$lastwithright = $rowswithright;
$lastwithleft = $rowswithleft - $rowswithright;
$lastwithpiezo = $rowswithpiezo - $rowswithleft;
$lastwithbattery = $rowswithbattery - $rowswithpiezo;