繁体   English   中英

“不能将字符串偏移用作数组”错误

[英]“Cannot use string offset as an array” error

无法想象这里有什么问题。 阅读以下人们的意见: http//informationideas.com/news/2006/06/14/fatal-error-cannot-use-string-offset-as-an-array-in/这里: 不能使用字符串偏移作为PHP中的数组

我有print_r() ed $entries的实际值(来自Google Calendar),它们都很好。

    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' ] 
                     );
     }

我究竟做错了什么?

转储$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] => 
)

我打赌也是

  • $ entries不是数组
  • $e中的一个或多个不是数组

尝试

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' ] );
}

如果你想真正做到这一点,你首先使用isset()array_key_exists()检查$e每个键(“startTime”等等array_key_exists()

你不需要foreach

$info = array( 
                  $entries[ 'title' ], 
                  $entries[ 'gd:when attr' ][ 'startTime' ], 
                  $entries[ 'gd:where attr' ][ 'valueString' ], 
                  $entries[ 'content' ] 
                 );

为了克服这个错误,首先检查它是一个数组还是网络然后进行数组元素操作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM