2010-01-13 30 views
3

無法找出這裏出了什麼問題。閱讀鄉親在這裏說什麼:http://informationideas.com/news/2006/06/14/fatal-error-cannot-use-string-offset-as-an-array-in/這裏:Cannot use string offset as an array in php「不能使用字符串偏移量作爲數組」錯誤

print_r() -ed在$entries(從谷歌日曆來)的實際值,它們無一不精。

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] => [email protected] 
      [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] => [email protected] 
     ) 

    [gCal:uid] => 
) 
+0

你能後的值的條目的print_r結果?這樣我們就知道了entires的結構。 – 2010-01-13 17:06:38

+0

你能告訴我們什麼print_r($ entries);打印? – antpaw 2010-01-13 17:08:29

+0

編輯添加$條目的轉儲;謝謝! – jerrygarciuh 2010-01-13 17:11:34

回答

18

我的選擇是,要麼

  • $條目是不是數組
  • 一個或$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' ]); 
} 

如果你想這樣做真的正確,你檢查的$e(「開始時間」等等),每個鍵首先使用isset()array_key_exists()

+1

謝謝Pekka!這不僅解決了這個問題,而且還闡明瞭我在錯誤假設下采取行動的代碼中的其他問題。多謝。 – jerrygarciuh 2010-01-13 17:19:37

1

你不需要在foreach

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

如果日曆恰好有多個條目,我不需要它嗎? – jerrygarciuh 2010-01-13 17:15:06

0

爲了克服這個錯誤首先檢查一下它的數組或線網是否然後做數組元素manipulat

相關問題