遇到PHP错误严重性:Notice消息:Undefined偏移量:0


A PHP Error was encountered Severity: Notice Message: Undefined offset: 0

我得到这个错误:

遇到PHP错误

严重性:注意

Message: Undefined offset: 0

文件名:控制器/web.php

行号:181

当我打开我的网站上最新的文章。

这是detail.php的代码:

            <?php
                // ...
                $p_satu = explode(' ',$c['tanggal']);
                $tgl = explode('-',$p_satu[0]); 
                $bulan = array('Jan','Feb','Mar', 'Apr', 'Mei', 'Jun','Jul','Ags','Sep','Okt', 'Nov','D');
            ?>
            <p><?php echo $tgl[2]; ?></p>
            <p><?php echo $bulan[($tgl[1]-1)]; ?></p>
        </div>
    </div>

这个web.php:

<?php
// ...
private function cookiesetter($kode = 0){
    if(!isset($_COOKIE[$kode])){
        $content = $this->blog_model->GetContent("where kode_content = '$kode'")->result_array();
        $result = $this->blog_model->UpdateData('content',array('counter' => ($content[0]['counter'])),array('kode_content'=>$kode));
        if($result == 1){
            setcookie($kode,"http://xxx.example.com", time()+3600);

以下是我的猜测。更新controllers/web.php

$result = 0;
if (!empty($content[0])) {
    $result = $this->blog_model->UpdateData('content',array('counter' => ($content[0]['counter'])),array('kode_content'=>$kode));
}
if ($result == 1) { 
    .....
}

如果这不能解决你的错误,让我知道从这个Line Number: 181 controllers/web.php的代码

所以你的数组tgl没有索引2。您必须检查$p_satu[0]

中的值。

这是访问array时常见的错误,如果在数组中访问undefined offset,或者在不存在的数组中访问key=>value,则会抛出此错误。例如:

$array = array(
    1=> 'hi',
    3 => 'bye'
);
echo $array[0]; //Throws the error undefined offset since '0' is not an index in the array

为了调试,我建议在索引为[0]的数组上执行print_r()var_dump,看看数组的内容是什么,因为它们可能不是您期望的。