出现Warning: Illegal string offset的解决方法

今天在做ecshop的时候,一段代码出现Warning: Illegal string offset的这样的错误,代码如下

$catlist = array_merge(cat_list(0, 0, false), array('-'), article_cat_list(0, 0, false));
 foreach($catlist as $key => $val)
 {

$val["cat_name"];
 }

上面代码可以输出值,但是报错Warning: Illegal string offset 'cat_name' in  ,原因是$catlist数组里面有空数组,空数组里没有cat_name这个字段。最终解决办法是在 foreach 下面加个判断 if(is_array($val)),这样就搞定了。

原文链接:http://www.bcty365.com/content-74-2008-1.html