继续我之前的question https://stackoverflow.com/questions/52397274/php-count-text-file-rows-based-on-date-and-time.
我有名为拒绝的文本日志文件。你可以看到here https://www.dropbox.com/s/ptp9jas8s1he9hh/Reject.txt?dl=0
如您所见,选项卡上有 4 个步骤:
1. Battery level
2. Piezo sound level
3. Left D3 (Ch 3) light intensity
4. Right D2 (Ch 1) light intensity
现在我想计算有条件的每一行:
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.
所以最后我可以知道有多少数量Reject Process。
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;
UPDATE
我需要读取最后一列值,示例:如果行的值位于左侧 D3 列,则对其进行计数。如果行的值位于 Piezo 列上,则对其进行计数。