我正在编写一个php代码来搜索文本文件中的不同变量....
数据在flatfile中逐行列出,数据列表的格式为:
date | time | ip | geo-location (city) | //url
数据保存为日志文件('track-log.txt')
我到目前为止的代码是:
$log_file = file_get_contents('track-log.txt');
$log_lines = explode(PHP_EOL, $log_file);
$log_lines = array_flip($log_lines);
unset($log_file);
这会将文本文件分解为行,然后向后翻转这些行,以便它们在数组$ log_lines [*]中列为文本文件中的最后一行,首先显示为$ log_lines [0]
我需要计算出有多少次出现的“约会”......
<..... lots of logs here .... then .....>
jan 1st 2012 | data.....
jan 1st 2012 | data ....
jan 1st 2012 | data ....
jan 1st 2012 | data ....
jan 1st 2012 | data ....
jan 2nd 2012 | data ....
jan 2nd 2012 | data ....
jan 2nd 2012 | data .... <end log>
想象一下这是日志的结尾....我想:
$count[0] = 3 // last 3x dates are the same
$count[1] = 5 // the 5x dates before that are the same
所以我可以使用
echo $count[0];
显示日志“日期”部分中最新值的数量。
我希望数组$ count [*]停止列出@ 7字符串......
$计数[0]; ......最多...... $ count [6]
将显示过去7天日志的页数
...
额外信息....日志中每行的日期格式为
sunday, january 22, 2012 | 16:14:36 | 82.**.***.*** | bolton | //page_url
日期格式始终与在每个日志行上写入每个日期的脚本相同
...
非常感谢。
答案 0 :(得分:1)
该函数更改了键和值,并且不反转数组..
$log_lines = array_flip($log_lines);
这样做
$log_lines = array_reverse($log_lines);
生成计数数组
$count = array();
$index = -1;
$last_date = false;
foreach ($log_lines as $lines) {
//sunday, january 22, 2012 | 16:14:36 | 82.**.***.*** | bolton | //page_url
list($date,) = explode("|",$lines,2); //extract date
if ($last_date == $date)
$count[$index]++;
else {
$last_date = $date;
$index++;
if ($index>=7) break; // 8. Date -> break
$count[$index] = 1;
}
}
答案 1 :(得分:0)
这应该是诀窍......
<?php
$log_file = file_get_contents('track-log.txt');
$log_lines = explode(PHP_EOL, $log_file);
$log_lines = array_reverse($log_lines);
unset($log_file);
$count = array();
//loop through and itterate the count array using the date as the unique key
foreach($log_lines as $line){
//explode this so we can use the date segment as a key
$pieces = explode('|',$line);
if (!isset($count[$pieces[0]])){
//break out if we've hit the cap size
if (count($count)>=7){break;}
$count[$pieces[0]] = 0;
}
$count[$pieces[0]]++;
}
//switch to a numeric index
$count = array_values($count);
?>
答案 2 :(得分:0)
这是解决问题的功能方法。
$lines = file('track-log.txt');
// This is a group discriminator
// It must return a 'key' which is what to group by,
// (in this case, by column 1 of the input line).
// as well as the orginal value.
function col1group($line){
$key = trim(current(explode('|', $line, 2)));
return array($key=>$line);
}
// $grouper is the group-descriminator function.
// we generate a new array with our keys to group by, then group them by
// key with array_merge_recursive
function array_groupby($array, $grouper) {
return call_user_func_array('array_merge_recursive', array_map($grouper, $array));
}
//This is all lines, in file order, grouped by date.
$linesbydate = array_groupby($lines, 'col1group');
// if you want just the last seven:
$last7dates = array_slice($linesbydate, -7);
// if you want numeric keys:
$last7groups = array_values($last7dates);
// if you want them in reverse order:
$last7groupsReversed = array_reverse($last7groups);
// desired output
var_dump($last7groupsReversed);
使用您为此程序提供的示例行将输出:
array(2) {
[0]=>
array(3) {
[0]=>
string(25) "jan 2nd 2012 | data ....
"
[1]=>
string(25) "jan 2nd 2012 | data ....
"
[2]=>
string(25) "jan 2nd 2012 | data ....
"
}
[1]=>
array(5) {
[0]=>
string(25) "jan 1st 2012 | data.....
"
[1]=>
string(25) "jan 1st 2012 | data ....
"
[2]=>
string(25) "jan 1st 2012 | data ....
"
[3]=>
string(25) "jan 1st 2012 | data ....
"
[4]=>
string(25) "jan 1st 2012 | data ....
"
}
}
如果您需要按其他条件进行分组,可以通过创建col1group()
之类的其他功能并将其传递给array_groupby()
来实现。您也可以在此功能中更改行本身(例如,调用rtrim()
删除换行符。)