“不能使用字符串偏移量作为数组"错误 [英] "Cannot use string offset as an array" error

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

问题描述

无法弄清楚这里出了什么问题.阅读人们在这里说的话:http://informationideas.com/news/2006/06/14/fatal-error-cannot-use-string-offset-as-an-array-in/ 和这里:无法在php中使用字符串偏移量作为数组>

我在 $entries(来自 Google 日历)中使用了 print_r()-ed 的实际值,它们都很好.

 foreach ( $entries as $e ) {$info = 数组();//添加以查看预声明是否有帮助$info = 数组($e['标题'],$e[ 'gd:when attr' ][ 'startTime' ],$e[ 'gd:where attr' ][ 'valueString' ],$e['内容']);}

我做错了什么?

$entries 的转储:

数组([id] =>http://www.google.com/calendar/feeds/u879een48cs77cp2rv7s05f5ps%40group.calendar.google.com/public/full/aev64a1c7kou9ige6n2mulm8mo[发表] =>2009-12-31T15:34:47.000Z[更新] =>2009-12-31T15:34:58.000Z[类别属性] =>大批([方案] =>http://schemas.google.com/g/2005#kind[术语] =>http://schemas.google.com/g/2005#event)[类别] =>[标题属性] =>大批([类型] =>文本)[标题] =>新年快乐![内容属性] =>大批([类型] =>文本)[内容] =>[链接] =>大批([0 attr] =>大批([rel] =>备用[类型] =>文本/html[参考] =>http://www.google.com/calendar/event?eid=YWV2NjRhMWM3a291OWlnZTZuMm11bG04bW8gdTg3OWVlbjQ4Y3M3N2NwMnJ2N3MwNWY1cHNAZw[标题] =>备用)[0] =>[1 属性] =>大批([rel] =>自己[类型] =>应用程序/原子+xml[参考] =>http://www.google.com/calendar/feeds/u879een48cs77cp2rv7s05f5ps%40group.calendar.google.com/public/full/aev64a1c7kou9ige6n2mulm8mo)[1] =>)[作者] =>大批([名称] =>新奥尔良公立学校家长指南)[gd:评论] =>大批([gd:feedLink attr] =>大批([参考] =>http://www.google.com/calendar/feeds/u879een48cs77cp2rv7s05f5ps%40group.calendar.google.com/public/full/aev64a1c7kou9ige6n2mulm8mo/comments)[gd:feedLink] =>)[gd:eventStatus attr] =>大批([值] =>http://schemas.google.com/g/2005#event.confirmed)[gd:eventStatus] =>[gd:where attr] =>大批([值字符串] =>)[gd:where] =>[gd:who attr] =>大批([电子邮件] =>u879een48cs77cp2rv7s05f5ps@group.calendar.google.com[rel] =>http://schemas.google.com/g/2005#event.organizer[值字符串] =>新奥尔良公立学校家长指南)[gd:who] =>[gd:when attr] =>大批([结束时间] =>2010-01-01[开始时间] =>2009-12-31)[gd:when] =>[gd:透明度属性] =>大批([值] =>http://schemas.google.com/g/2005#event.opaque)[gd:透明度] =>[gCal:anyoneCanAddSelf attr] =>大批([值] =>错误的)[gCal:anyoneCanAddSelf] =>[gCal:guestsCanInviteOthers attr] =>大批([值] =>真的)[gCal:guestsCanInviteOthers] =>[gCal:guestsCanModify attr] =>大批([值] =>错误的)[gCal:guestsCanModify] =>[gCal:guestsCanSeeGuests attr] =>大批([值] =>真的)[gCal:guestsCanSeeGuests] =>[gCal:sequence attr] =>大批([值] =>2)[gCal:sequence] =>[gCal:uid attr] =>大批([值] =>aev64a1c7kou9ige6n2mulm8mo@google.com)[gCal:uid] =>)

解决方案

我敢打赌

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

试试

foreach ( $entries as $e ) {$info = 数组();//添加以查看预声明是否有帮助if (is_array($e))//只有在 $e 实际上是一个数组时才继续$info = array( $e[ 'title' ],$e[ 'gd:when attr' ][ 'startTime' ],$e[ 'gd:where attr' ][ 'valueString' ],$e['内容']);}

如果你想真正正确地做到这一点,你首先使用 isset() 检查 $e(startTime"等)的每个键>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().

这篇关于“不能使用字符串偏移量作为数组"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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