PHP循环中未定义的偏移量错误

PHP循环中未定义的偏移量错误,php,Php,我正在查看我的httpd错误日志,并一次又一次地注意到这个错误 未定义的偏移量:第43行的/usr/home/*********/*********/******/******/index.php中有1个偏移量 我希望有人能检查一下我的代码,看看他们是否发现了导致这个错误的任何问题 <?php $files = scandir('movies/'); foreach($files as $file) { if ($file === '.'

我正在查看我的httpd错误日志,并一次又一次地注意到这个错误

未定义的偏移量:第43行的/usr/home/*********/*********/******/******/index.php中有1个偏移量

我希望有人能检查一下我的代码,看看他们是否发现了导致这个错误的任何问题

<?php $files = scandir('movies/');
            foreach($files as $file) {
              if ($file === '.' or $file === '..') continue;
              $t = str_replace("-", " ", $file);
              $section = explode(';', $t);
              $section = explode('.', $section[1]);
              $t = explode(';', $t);
              $t = $t[0]; 

代码正在站点上运行,但出现了错误,因为在爆炸后,您可能只有带有
[0]
键的数组,这意味着您没有
$t
中的code>,整个
$t
进入第一个
$section
作业

永远不要假设在代码开发过程中,您总是拥有准确的数据。你在给自己设陷阱。我建议的解决方案大致如下:

$section = explode(';', $t);
$section = explode('.', count($section) > 1 ? $section[1] : $section[0]);


所以,在尝试分解它之前,请检查您是否有
$section[1]
?非常直截了当。可能重复感谢您的快速回复,您是正确的,有一些文件的命名方案错误。我只是放了一个简单的if语句来检查
$section[1]
是否为空。我也处理了名字的问题。谢谢你给我的所有帮助
$section = explode(';', $t);
$section = explode('.', count($section) > 1 ? $section[1] : $section[0]);
$section = explode(';', $t);
if(count($section) === 0) {
    // debug code, warnings, reports, Exceptions etc.
} else {

}