dongzhang5787 2013-04-28 14:53
浏览 36
已采纳

警告:PHP 5.4中的非法字符串偏移[重复]

This question already has an answer here:

I upgraded to PHP 5.4 today and I am receiving some strange warnings:

Warning: Illegal string offset 'quote1' in file.php on line 110
Warning: Illegal string offset 'quote1_title' in file.php on line 111

Those lines are this part of the code:

for($i = 0; $i < 3; $i++) {
    $tmp_url = $meta['quote'. ($i+1)];
    $tmp_title = $meta['quote' . ($i+1) .'_title'];

    if(!empty($tmp_url) || !empty($tmp_title)) {
        $quotes[$src_cnt] = array();
        $quotes[$src_cnt]['url'] = $tmp_url;
        $quotes[$src_cnt]['title'] = $tmp_title;
        $src_cnt++;
    }
}

So the $tmp_url and $tmp_title line.

Why am I receiving this odd warning and what is the solution?

Update:

This code is being used as a Wordpress plugin. $meta includes:

$meta = get_post_meta($post->ID,'_quote_source',TRUE);

So I am suspecting that whenever the quotes fields are empty, this warning appears. Is there any way that I can fix this for when the fields are empty?

</div>
  • 写回答

2条回答 默认 最新

  • douguanyun2169 2013-04-28 15:13
    关注

    You need to make sure, that $meta is actually of type array. The warning explicitly tells you, that $meta seems to be a string and not an array

    Illegal string offset
            ^^^^^^
    

    To avoid this error you may also check for the needed fields

    for($i = 0; $i < 3; $i++) {
        if ( !is_array($meta) || !array_key_exists('quote'. ($i+1), $meta) ){
             continue;
        }
        // your code
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题