PHP 5.4警告:非法字符串偏移'字段'

时间:2015-09-02 06:43:55

标签: php

我不明白为什么这段代码会给php 5.4发出警告?我该如何解决这个问题?

function load(){
    for ($i = 0, $n = sizeof($messages); $i < $n; $i++){
        $this->add($messages[$i]['field'], $messages[$i]['type']);                       
    }
}

function add($field, $type){    
    $this->messages[] = array('field' => $field, 'type' => $type);
}

1 个答案:

答案 0 :(得分:1)

试试这个

function load(){
            for ($i = 0, $n = sizeof($messages); $i < $n; $i++){
                if(isset($messages[$i]['field']) && isset($messages[$i] ['field'])){
                $this->add($messages[$i]['field'], $messages[$i]['type']);
                }
               }                       
           }

function add($field, $type){    
        $this->messages[] = array('field' => $field, 'type' => $type);
        }