首页 > 解决方案 > 如何在正则表达式中修复此错误“未定义的偏移量:”?

问题描述

对于数据中的每一行,我都会收到此错误。所以大约 500 次,但每次都有另一个未定义的偏移量。这是我的代码:

$fl_array = preg_grep('/^\d+\s('. $SelectedTime .':)\d+/', explode("\n", $finalLog));
$count = count($fl_array);

for ($x = 0; $x < $count; $x++)
{
    echo "$fl_array[$x] \n";
}

标签: phpregex

解决方案


如此处所写 - http://php.net/manual/en/function.preg-grep.phpReturns an array indexed using the keys from the input array.所以您的密钥可能有问题:

if (isset($fl_array[$x])) {
    echo "$fl_array[$x] \n";
}

推荐阅读