"不能使用字符串数组&QUOT抵消;错误 [英] "Cannot use string offset as an array" error

查看:192
本文介绍了"不能使用字符串数组&QUOT抵消;错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想不通什么是错在这里。阅读乡亲在这里说什么:<一href=\"http://informationideas.com/news/2006/06/14/fatal-error-cannot-use-string-offset-as-an-array-in/\" rel=\"nofollow\">http://informationideas.com/news/2006/06/14/fatal-error-cannot-use-string-offset-as-an-array-in/在这里:不能使用字符串作为PHP中的数组

的print_r() -ed中的实际值 $项目(从谷歌日历来),他们无一不精。

 的foreach($条目$ E){
        $信息=阵列(); //添加看看是否pre-声明帮助
        $信息=阵列(
                      $ E ['标题'],
                      $ E ['的gd:当ATTR'] ['的startTime'],
                      $ E ['的gd:其中attr'] ['的valueString'],
                      $ E ['内容']
                     );
     }

我在做什么错了?

转储 $项目

 阵列

    [ID] =&GT; http://www.google.com/calendar/feeds/u879een48cs77cp2rv7s05f5ps%40group.calendar.google.com/public/full/aev64a1c7kou9ige6n2mulm8mo
    [出版] =&GT; 2009-12-31T15:34:47.000Z
    [更新] =&GT; 2009-12-31T15:34:58.000Z
    【类别ATTR] =&GT;排列
        (
            [方案] =&GT; http://schemas.google.com/g/2005#kind
            [术语] =&GT; http://schemas.google.com/g/2005#event
        )    【类别】= GT;
    [标题ATTR] =&GT;排列
        (
            [类型] = GT;文本
        )    [标题] =&GT;新年快乐!
    [内容ATTR] =&GT;排列
        (
            [类型] = GT;文本
        )    [内容] =&GT;
    [链接] =&GT;排列
        (
            [0 ATTR] =&GT;排列
                (
                    [REL] =&GT;备用
                    [类型] = GT; text / html的
                    [HREF =&GT; http://www.google.com/calendar/event?eid=YWV2NjRhMWM3a291OWlnZTZuMm11bG04bW8gdTg3OWVlbjQ4Y3M3N2NwMnJ2N3MwNWY1cHNAZw
                    [标题] =&GT;备用
                )            [0] =&GT;
            [1 ATTR] =&GT;排列
                (
                    [REL] =&GT;自
                    [类型] = GT;应用程序/原子+ XML
                    [HREF =&GT; http://www.google.com/calendar/feeds/u879een48cs77cp2rv7s05f5ps%40group.calendar.google.com/public/full/aev64a1c7kou9ige6n2mulm8mo
                )            [1] =&GT;
        )    [作者] =&GT;排列
        (
            [名] =&GT;新奥尔良家长引导到公立学校
        )    [GD:评论] =&GT;排列
        (
            [GD:feedLink ATTR] =&GT;排列
                (
                    [HREF =&GT; http://www.google.com/calendar/feeds/u879een48cs77cp2rv7s05f5ps%40group.calendar.google.com/public/full/aev64a1c7kou9ige6n2mulm8mo/comments
                )            [GD:feedLink] =&GT;
        )    [GD:eventStatus ATTR] =&GT;排列
        (
            [值] =&GT; http://schemas.google.com/g/2005#event.confirmed
        )    [GD:eventStatus] =&GT;
    [GD:其中attr] =&GT;排列
        (
            [的valueString] =&GT;
        )    [GD:其中=&GT;
    [GD:谁ATTR] =&GT;排列
        (
            [邮件] =&GT; u879een48cs77cp2rv7s05f5ps@group.calendar.google.com
            [REL] =&GT; http://schemas.google.com/g/2005#event.organizer
            [的valueString] =&GT;新奥尔良家长引导到公立学校
        )    [GD:谁] =&GT;
    [GD:当ATTR =&GT;排列
        (
            [ENDTIME] =&GT; 2010-01-01
            [startTime时] =&GT; 2009-12-31
        )    [GD:当=&GT;
    [GD:透明ATTR] =&GT;排列
        (
            [值] =&GT; http://schemas.google.com/g/2005#event.opaque
        )    [GD:透明] =&GT;
    [克卡:anyoneCanAddSelf ATTR] =&GT;排列
        (
            [值] =&GT;假
        )    [克卡:anyoneCanAddSelf] =&GT;
    [克卡:guestsCanInviteOthers ATTR] =&GT;排列
        (
            [值] =&GT;真正
        )    [克卡:guestsCanInviteOthers] =&GT;
    [克卡:guestsCanModify ATTR] =&GT;排列
        (
            [值] =&GT;假
        )    [克卡:guestsCanModify] =&GT;
    [克卡:guestsCanSeeGuests ATTR] =&GT;排列
        (
            [值] =&GT;真正
        )    [克卡:guestsCanSeeGuests] =&GT;
    [克卡:序列ATTR] =&GT;排列
        (
            [值] =&GT; 2
        )    [克卡:序列] =&GT;
    [克卡:UID ATTR] =&GT;排列
        (
            [值] =&GT; aev64a1c7kou9ige6n2mulm8mo@google.com
        )    [克卡:UID] =&GT;


我敢打赌,要么


  • $项不是数组

  • 中的一个或多个不阵列

尝试

 的foreach($条目$ E){
    $信息=阵列(); //添加看看是否pre-声明帮助
    如果(is_array($ E))//只有去当$Ë实际上是一个数组
    $信息=阵列($ E ['标题'],
                   $ E ['的gd:当ATTR'] ['的startTime'],
                   $ E ['的gd:其中attr'] ['的valueString'],
                   $ E ['内容']);
}

如果你想这样做真的很不好,你检查(startTime时等)首先使用的每个键使用isset () array_key_exists()

Can't figure what is wrong here. Read what folks are saying here: http://informationideas.com/news/2006/06/14/fatal-error-cannot-use-string-offset-as-an-array-in/ and here: Cannot use string offset as an array in php

I have print_r()-ed the actual values in $entries (coming from Google Calendar) and they are all fine.

    foreach ( $entries as $e ) {
        $info = array(); // added to see if pre-declaration helps
        $info = array( 
                      $e[ 'title' ], 
                      $e[ 'gd:when attr' ][ 'startTime' ], 
                      $e[ 'gd:where attr' ][ 'valueString' ], 
                      $e[ 'content' ] 
                     );
     }

What am I doing wrong?

Dump of $entries:

Array
(
    [id] => http://www.google.com/calendar/feeds/u879een48cs77cp2rv7s05f5ps%40group.calendar.google.com/public/full/aev64a1c7kou9ige6n2mulm8mo
    [published] => 2009-12-31T15:34:47.000Z
    [updated] => 2009-12-31T15:34:58.000Z
    [category attr] => Array
        (
            [scheme] => http://schemas.google.com/g/2005#kind
            [term] => http://schemas.google.com/g/2005#event
        )

    [category] => 
    [title attr] => Array
        (
            [type] => text
        )

    [title] => Happy New Year!
    [content attr] => Array
        (
            [type] => text
        )

    [content] => 
    [link] => Array
        (
            [0 attr] => Array
                (
                    [rel] => alternate
                    [type] => text/html
                    [href] => http://www.google.com/calendar/event?eid=YWV2NjRhMWM3a291OWlnZTZuMm11bG04bW8gdTg3OWVlbjQ4Y3M3N2NwMnJ2N3MwNWY1cHNAZw
                    [title] => alternate
                )

            [0] => 
            [1 attr] => Array
                (
                    [rel] => self
                    [type] => application/atom+xml
                    [href] => http://www.google.com/calendar/feeds/u879een48cs77cp2rv7s05f5ps%40group.calendar.google.com/public/full/aev64a1c7kou9ige6n2mulm8mo
                )

            [1] => 
        )

    [author] => Array
        (
            [name] => New Orleans Parents Guide to Public Schools
        )

    [gd:comments] => Array
        (
            [gd:feedLink attr] => Array
                (
                    [href] => http://www.google.com/calendar/feeds/u879een48cs77cp2rv7s05f5ps%40group.calendar.google.com/public/full/aev64a1c7kou9ige6n2mulm8mo/comments
                )

            [gd:feedLink] => 
        )

    [gd:eventStatus attr] => Array
        (
            [value] => http://schemas.google.com/g/2005#event.confirmed
        )

    [gd:eventStatus] => 
    [gd:where attr] => Array
        (
            [valueString] => 
        )

    [gd:where] => 
    [gd:who attr] => Array
        (
            [email] => u879een48cs77cp2rv7s05f5ps@group.calendar.google.com
            [rel] => http://schemas.google.com/g/2005#event.organizer
            [valueString] => New Orleans Parents Guide to Public Schools
        )

    [gd:who] => 
    [gd:when attr] => Array
        (
            [endTime] => 2010-01-01
            [startTime] => 2009-12-31
        )

    [gd:when] => 
    [gd:transparency attr] => Array
        (
            [value] => http://schemas.google.com/g/2005#event.opaque
        )

    [gd:transparency] => 
    [gCal:anyoneCanAddSelf attr] => Array
        (
            [value] => false
        )

    [gCal:anyoneCanAddSelf] => 
    [gCal:guestsCanInviteOthers attr] => Array
        (
            [value] => true
        )

    [gCal:guestsCanInviteOthers] => 
    [gCal:guestsCanModify attr] => Array
        (
            [value] => false
        )

    [gCal:guestsCanModify] => 
    [gCal:guestsCanSeeGuests attr] => Array
        (
            [value] => true
        )

    [gCal:guestsCanSeeGuests] => 
    [gCal:sequence attr] => Array
        (
            [value] => 2
        )

    [gCal:sequence] => 
    [gCal:uid attr] => Array
        (
            [value] => aev64a1c7kou9ige6n2mulm8mo@google.com
        )

    [gCal:uid] => 
)

解决方案

My bet is that either

  • $entries is not an array
  • one or multiple of $e are not arrays

try

foreach ( $entries as $e ) {
    $info = array(); // added to see if pre-declaration helps
    if (is_array($e)) // only go on if $e is actually an array
    $info = array( $e[ 'title' ], 
                   $e[ 'gd:when attr' ][ 'startTime' ], 
                   $e[ 'gd:where attr' ][ 'valueString' ], 
                   $e[ 'content' ] );
}

If you want to do it really properly, you check for each key of $e ("startTime" and so on) first using isset() or array_key_exists().

这篇关于&QUOT;不能使用字符串数组&QUOT抵消;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆