PHP基于值列计算文本文件上的行数
继续我以前的工作 我有一个名为reject的文本日志文件。你可以看到 如您所见,选项卡上有4个步骤,称为:PHP基于值列计算文本文件上的行数,php,Php,继续我以前的工作 我有一个名为reject的文本日志文件。你可以看到 如您所见,选项卡上有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, W
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.
所以最后我可以知道有多少数量的拒绝过程
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;
$fromDateTime=新日期时间('Wed,Sep 19 2018 08:00:00');
$toDateTime=新日期时间(“2018年9月19日星期三19:59:00”);
$file=file_get_contents('reject.txt');
$lines=explode(“\n”,$file);
//柜台
$rowsintimespan=0;
//从第16行开始逐行执行(数组索引15)
对于($i=15;$i$fromDateTime){
$rowsintimespan++;//在timespan中计数
}
}
//调试输出
echo$rowsintimespan;
更新我需要读取最后一列的值,例如:如果row的值在D3列的左边,那么计算它。如果行的值在列Piezo上,则对其进行计数
如果您可以将列写为键,那么这应该按照您所描述的那样工作:
$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);
只需将您的行按选项卡拆分,然后检查是否存在错误!空() 包含所有列的完整示例: 您还可以迭代每个头并创建一个整洁的数组,如中所示
嗨,Bam,我更新了我上面的问题,请看一看,结果是错误的,请参考我共享的日志文件。
lastwithright:84 lastwithleft:-57 lastwithpiezo:52 lastwithbattery:5
我尝试手动计数,正确的值应该是:`Battery:5 Right D2:1 Left D3:27 Piezo:52`@Hidayurie Dave我不这么认为,工作完美:我真的很想知道你在做什么。您已经注意到这里的代码片段不完整,对吗?你读过吗,全部代码都在ideone上?因为它不需要修改就可以工作。嗨,Bam,我刚刚尝试在ideone.com/9HivaU上使用你的代码(我使用的是你的纯代码),但仍然得到了错误的值,在时间跨度内:84行带电池:84行带压电:79行带左侧:27行带右侧:83行带右侧:83行带左侧:-56行带压电:52行带电池:5
Hi Alex,我收到错误通知:未定义索引:第39行C:\xampp\htdocs\test\test.php中的日期通知:第39行C:\xampp\htdocs\test\test.php中的时间通知:未定义索引:第39行C:\xampp\htdocs\test\test.php中的电池通知:未定义索引:第39行C:\xampp\htdocs\test\test\test.php中的压电元件通知:未定义索引:第39行C:\xampp\htdocs\test\test.php中的leftD3注意:第39行C:\xampp\htdocs\test\test.php中的未定义索引:rightD2
抱歉,Dave,我报告时出错,我编辑了代码以避免注意到我Alex,我尝试手动计数,唯一正确的1是Right D2=1,另一个是错误的。这是我的手动计数日志文件:电池:5右D2:1左D3:27压电:52
@HiDayurieDave我终于明白你的要求了。尝试此更新的代码Hi Alex,刚才选中的只有Right D2
是正确的。另一个是错误的。应该是电池:5右D2:1左D3:26压电:52
所以您只想计算一行的最后一个值?即,如果设置了batterylevel和piezo,则仅计算piezo?如果电池,压电和左设置,然后只计算左?是的。。。没错,先生
Array
(
[date] => 0
[time] => 0
[battery] => 4
[piezo] => 31
[leftD3] => 17
[rightD2] => 1
)
//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
// 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);
$lastwithright = $rowswithright;
$lastwithleft = $rowswithleft - $rowswithright;
$lastwithpiezo = $rowswithpiezo - $rowswithleft;
$lastwithbattery = $rowswithbattery - $rowswithpiezo;