首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于值列的PHP文本文件中的行/行计数

基于值列的PHP文本文件中的行/行计数
EN

Stack Overflow用户
提问于 2018-09-19 10:24:46
回答 2查看 427关注 0票数 0

继续我以前的问题

我有一个名为“拒绝”的文本文件。你可以看到它,这里

如您所见,选项卡上有4个步骤,名为:

代码语言:javascript
复制
1. Battery level
2. Piezo sound level
3. Left D3  (Ch 3) light intensity
4. Right D2  (Ch 1) light intensity

现在,我想用条件来计算每一行:

代码语言:javascript
复制
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.

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

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
$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列上,则计数它。如果行的值在Piezo列上,则计数它。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-19 11:11:33

如果您可以将列写为键,那么这应该像您所描述的那样起作用:

代码语言:javascript
复制
$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);

这将打印出来:

代码语言:javascript
复制
Array
(
    [date] => 0
    [time] => 0
    [battery] => 4
    [piezo] => 31
    [leftD3] => 17
    [rightD2] => 1
)
票数 1
EN

Stack Overflow用户

发布于 2018-09-19 11:11:47

只需按选项卡将行拆分,并检查是否为空();

代码语言:javascript
复制
//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

生产:

代码语言:javascript
复制
In Timespan: 84
Rows With Battery: 84

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

此外,您还可以遍历每个标头并创建一个整洁的数组,就像我在https://ideone.com/YUU1jP中显示的那样。

代码语言:javascript
复制
// 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个电池柱,然后你知道你有多少个电池柱,还有多少个最后的压电柱。

因此:

代码语言:javascript
复制
$lastwithright = $rowswithright;
$lastwithleft = $rowswithleft - $rowswithright;
$lastwithpiezo = $rowswithpiezo - $rowswithleft;
$lastwithbattery = $rowswithbattery - $rowswithpiezo;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52403677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档