Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.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 如何忽略此函数中基于特定语句的行,该语句从文本文件读取数据?_Php - Fatal编程技术网

Php 如何忽略此函数中基于特定语句的行,该语句从文本文件读取数据?

Php 如何忽略此函数中基于特定语句的行,该语句从文本文件读取数据?,php,Php,关于函数的信息:当前它获取最后20行,并根据行中请求的列显示信息 我想做什么 我想忽略包含相同第5列和第13列的行。 示例:应忽略下面两行,因为第一行中的第5列与第13列匹配,第二行与第一行相同,因为第5列中的第一个名称与第13列的名称匹配 1,42,16, 201,stackoverflow_user, 1, 6762160, 39799, 9817242, 6762160, 39884, 10010545,stackoverflow_user, 2, 1351147, 1165, 48325

关于函数的信息:当前它获取最后20行,并根据行中请求的列显示信息

我想做什么

我想忽略包含相同第5列和第13列的行。 示例:应忽略下面两行,因为第一行中的第5列与第13列匹配,第二行与第一行相同,因为第5列中的第一个名称与第13列的名称匹配

1,42,16, 201,stackoverflow_user, 1, 6762160, 39799, 9817242, 6762160, 39884, 10010545,stackoverflow_user, 2, 1351147, 1165, 483259, 1351147, 1115, 241630, 0 
1,46,27, 201,[stackoverflow_user | stackoverflow_userother], 1, 4078465, 286991, 1594830, 4078465, 287036, 1643156,stackoverflow_user, 2, 1357147, 1115, 241630, 1357147, 1065, 120815, 0 
因此,脚本应该获取额外的行并忽略列匹配的行,直到它可以显示20个有效行

以下是我的实际功能:


请帮助我如何在我当前的功能上执行此操作。谢谢

下面的一些代码将测试列5和列13是否相同,如果相同,则跳过该行。它使用累加器来记录有多少有效行。请参阅代码,以获取有关它正在执行的操作的注释

# read a file into an array
$lines = file('C:/path/to/file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

# flip our array over so the last lines of the file are first.
$lines = array_reverse($lines);
$n = 0;
$wanted = 20; # or however many lines you want.
$content = '';

foreach ($lines as $l) {
   # treat the data as comma-separated values
   $arr = explode(",", $l);
   # if col 5 has multiple values, take the first one
   if (preg_match("/\[(.+?) \|/", $arr[4], $matches)) {
       $arr[4] = $matches[1];
   }
   # is arr[4] the same as arr[12]?
   if ($arr[4] !== $arr[12]) {
       # these two are not equal, so use these values
       $data = array('rank-pos' => $n++, 'rank-name' => $arr[4], 'rank-dmuser' => $arr[12]);
       $content .= Template::Load('rankinguserdm-' . ($n % 2 == 1 ? 2 : 1), $data);
   }
   # have we got enough data?
   if ($n === $wanted) {
       break;
   }
}
# check if we got enough data from the loop
if ($n < $wanted) {
    die("We couldn't get enough data!");
}

要得到20条有效行,只需继续循环直到得到20条

$lastline=count($lines);
$dsp=0;

while(dsp<20){
      //check to see if there are any more lines
      if(!$lines[$lastline-$dsp]){
              echo 'oops, we ran out of lines';
              break;}
      $l=$lines[$lastline-$dsp];
        # treat the data as comma-separated values
        $arr = explode(",", $l);
        # if col 5 has multiple values, take the first one
        if (preg_match("/\[(.+?) \|/", $arr[4], $matches)) {
            $arr[4] = $matches[1];
            }
        //skip if 5==13
        if($arr[4]==$arr[12])continue;

        //else: add one to counter
        $dsp++;

        # store the data we want in an output array.
        $data = array('rank-pos' => $n++, 'rank-name' => $arr[4], 'rank-dmuser' => $arr[12]);
    $content .= Template::Load('rankinguserdm-' . ($n % 2 == 1 ? 2 : 1), $data);
    }

他只从文件中获取了20行:$last_ten=array_slice$lines,-20;所以,如果一个失败了,他就再也不能拥有一个了。正因为如此,一切都一团糟。非常感谢,但由于某些原因,它显示不正确:它开始-第1、第2、第3、第25、第29次,此时它应该只显示1到20。是什么原因造成的?您的要求是什么?您想要10行有效行还是20行有效行?它是10,这就是为什么我在n=10时加入了中断。@Monk25您还应该发布一段您正在使用的数据文件,以便我们可以检查数据是否导致问题。实际上您是对的。我使用了10行,但由于2行有相同的列,所以它只显示8行。是否可以循环直到显示10条有效行(如果存在)?而且,实际上如果文本文件不存在或没有足够的行,它不会产生错误,对吗?所以我可以放心,如果没有足够的行/0行或文件不存在,它将工作,或者干脆不显示任何内容?OP已经有$n作为累加器-您可以使用它。
$lastline=count($lines);
$dsp=0;

while(dsp<20){
      //check to see if there are any more lines
      if(!$lines[$lastline-$dsp]){
              echo 'oops, we ran out of lines';
              break;}
      $l=$lines[$lastline-$dsp];
        # treat the data as comma-separated values
        $arr = explode(",", $l);
        # if col 5 has multiple values, take the first one
        if (preg_match("/\[(.+?) \|/", $arr[4], $matches)) {
            $arr[4] = $matches[1];
            }
        //skip if 5==13
        if($arr[4]==$arr[12])continue;

        //else: add one to counter
        $dsp++;

        # store the data we want in an output array.
        $data = array('rank-pos' => $n++, 'rank-name' => $arr[4], 'rank-dmuser' => $arr[12]);
    $content .= Template::Load('rankinguserdm-' . ($n % 2 == 1 ? 2 : 1), $data);
    }