php - PHP错误处理-未定义偏移

标签 php error-handling apache2

我已经检查了我的apache错误日志,并且看到了如下多个消息:

client 108.162.246.190] PHP Notice:  Undefined offset: 4 in /var/www/html/search.php on line 142
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice:  Undefined offset: 5 in /var/www/html/search.php on line 142
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice:  Undefined offset: 5 in /var/www/html/search.php on line 143
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice:  Undefined offset: 6 in /var/www/html/search.php on line 142
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice:  Undefined offset: 6 in /var/www/html/search.php on line 143
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice:  Undefined offset: 7 in /var/www/html/search.php on line 142
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice:  Undefined offset: 7 in /var/www/html/search.php on line 143
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice:  Undefined offset: 8 in /var/www/html/search.php on line 142
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice:  Undefined offset: 8 in /var/www/html/search.php on line 143
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice:  Undefined offset: 9 in /var/www/html/search.php on line 142
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice:  Undefined offset: 9 in /var/www/html/search.php on line 143

围绕142-143行的代码如下所示(实际的142-143行是内爆的):
    if ($nuorodoshut != NULL)
        {
            foreach ($nuorodoshut as $key => $nuorodahut) 
            {   
            $keywords = explode(' ', $qsvarus);
            $title[$key] = preg_replace('/\b('.implode('|', $keywords).')\b(?![^<]*[>])/i', '<b>$0</b>', $title[$key]);
            $infoo[$key] = preg_replace('/\b('.implode('|', $keywords).')\b(?![^<]*[>])/i', '<b>$0</b>', $infoo[$key]);
            echo '<tr><td><h3>';
            echo str_replace('<a href="/', '<a href="/host/', $nuorodahut->innertext) . '</h3>';
            echo $aprasymashut[$key]->innertext . '<br>';

            }
}

我只是无法处理此错误。任何帮助,将不胜感激

最佳答案

错误消息告诉您,当$title[$key]$infoo[$key]-$key时,没有59。如果这两个数组都应具有与$nuorodoshut相同的索引,则在创建数组时出了点问题。

如果可以确定它们不应该包含这些元素,则在使用这些值之前,需要先进行检查,例如

$title[$key] = isset($title[$key]) ? preg_replace('/\b('.implode('|', $keywords).')\b(?![^<]*[>])/i', '<b>$0</b>', $title[$key]) : '';
$infoo[$key] = isset($infoo[$key]) ? preg_replace('/\b('.implode('|', $keywords).')\b(?![^<]*[>])/i', '<b>$0</b>', $infoo[$key]) : '';

关于php - PHP错误处理-未定义偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20850090/

相关文章:

php - 如何仅从 'list' 表中获取行。不是父表

r - 函数可以通过tryCatch和好友粉碎吗?

php - 404 错误但来自 Codeigniter Framework 中的 JQuery Ajax 的正确响应

perl - 如何从使用 su -c 在管道中执行的命令中获取 Perl 中的 STDERR

css - Pagespeed 和 css 交付优化

linux - 错误 libapache2-mod-php5 软件包 ubuntu

php - 如何解决这个错误 "Undefined variable: id"

php - url 中的 htaccess rewriterule 2 参数

php - 测试具有模型作为依赖项的 laravel 存储库

Scala try/catch vs 将来恢复不同的处理异常