Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在数组中搜索最近7次出现的情况_Php_Arrays_Search_Flat File - Fatal编程技术网

Php 在数组中搜索最近7次出现的情况

Php 在数组中搜索最近7次出现的情况,php,arrays,search,flat-file,Php,Arrays,Search,Flat File,我正在编写一个php代码来搜索文本文件中的不同变量 数据在平面文件中逐行列出,数据列出格式为: 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_f

我正在编写一个php代码来搜索文本文件中的不同变量

数据在平面文件中逐行列出,数据列出格式为:

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\U lines[*]中,并首先显示为$log\U 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>
所以我可以用

echo $count[0];
在日志的“日期”部分显示最近值的数量

我希望数组$count[*]停止列出@7个字符串

$count[0]。。。最多$计数[6]

它将显示最近7天日志的页数

额外信息。。。。日志中每行的日期格式为

sunday, january 22, 2012 | 16:14:36 | 82.**.***.*** | bolton | //page_url
日期格式始终与在每个日志行上写入每个日期的脚本相同


..

这应该是一个技巧

<?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);

?>

这应该是一个技巧

<?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);

?>

该函数更改键和值,并且不反转数组

 $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;
     }
}

该函数更改键和值,并且不反转数组

 $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;
     }
}

以下是解决您的问题的功能性方法

$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\u groupby()
。您还可以在此函数中更改行本身(例如,调用
rtrim()
删除换行)。

以下是解决问题的函数方法

$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\u groupby()
。您还可以在此函数中更改行本身(例如,调用
rtrim()
删除换行)。

您的启动代码不正确。交换键和值。您只需要
$lines=file('track-log.txt')$行=数组\反向($line)我需要explode命令,以逐行分割主输入字符串。。。。。除非您有更好的编码方法……?
file()
函数为您提供一个行数组。不需要
explode()
。您的启动代码不正确。交换键和值。您只需要
$lines=file('track-log.txt')$行=数组\反向($line)我需要explode命令,以逐行分割主输入字符串。。。。。除非您有更好的编码方法……?
file()
函数为您提供一个行数组。不需要
explode()
。你数到7。一天只有一次,即使有5次,也能抓到劳森。我已经进行了相应的编辑(将中断条件移动到元素创建步骤中。dang!!我已经编辑了第一个版本来删除它!再次感谢:)编码方面的小问题。。。。当写入数据行的脚本先写入数据,然后写入新行字符时,您编写的脚本将$count[0]计为空行,并列出值“1”。。。。将(count($count)>=7)更改为8,并忽略了数组$count[0],只需使用1-7即可,非常感谢您还可以修剪()日志内容以删除尾随/前导行。很高兴它起作用了你数到7。一天只有一次,即使有5次,也能抓到劳森。我已经进行了相应的编辑(将中断条件移动到元素创建步骤中。dang!!我已经编辑了第一个版本来删除它!再次感谢:)编码方面的小问题。。。。当写入数据行的脚本先写入数据,然后写入新行字符时,您编写的脚本将$count[0]计为空行,并列出值“1”。。。。将(count($count)>=7)更改为8,并忽略了数组$count[0],只需使用1-7即可,非常感谢您还可以修剪()日志内容以删除尾随/前导行。很高兴它对rauchen起到了+1的作用:(a)首先捕获数组_反向错误,(b)在my和Leonard的原始答案中捕获过早中断子句,以及(c)在explode子句中使用limit参数(这在优化方面可能有些过分,但仍然是很好的做法,在具有长日志项的扁平文件上可能会有所不同)。我仍然更喜欢我的方法(直接使用日期作为唯一键,然后在最后使用数组赋值),但这同样好,而且是一个更好的总体答案。+1对rauchen来说:(a)首先捕捉数组反向错误,(b)在我和Leonard的原始答案中捕捉过早中断子句,以及(c)在explode子句中使用limit参数(这在优化方面可能有些过分,但仍然是一种良好的做法,在具有长日志条目的扁平文件上可能会有所不同)。我仍然更喜欢我的方法(直接使用日期作为唯一键,然后在最后使用array_value-ing),但这同样好,而且是一个更好的总体答案。嗨,francis,我设置了错误列表,但是当我使用您的代码时,我只会得到一个空白页?对不起,这里没问题。我已将我的输出附加到答案。你至少理解代码了吗?是的,我理解代码,但它不起作用,但这个问题的另一个答案起作用了,我现在已经完成了今天的脚本。。。。。我可能也会开始下一个tonite的工作lol谢谢matehi francis我已经设置了错误列表,但是当我使用你的代码时,我只会得到一个空白页?对不起,这里没问题。我已将我的输出附加到答案。你至少理解代码了吗?是的,我理解代码,但它不起作用,但这个问题的另一个答案起作用了,我现在已经完成了今天的脚本。。。。。我可能也会开始下一个tonite的工作哈哈,谢谢你,伙计