3

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] => [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] => 
)
6
  • Can you post the results of the print_r of the values in entries? That way we know what the structure of entires is. Jan 13, 2010 at 17:06
  • can you show us what print_r($entries); prints?
    – antpaw
    Jan 13, 2010 at 17:08
  • Edited to add dump of $entries; thx! Jan 13, 2010 at 17:11
  • 2
    Hah, I won my bet! When do I get my beer? :)
    – Pekka
    Jan 13, 2010 at 17:12
  • Seriously, this is just one calendar entry. If you run a foreach on it, you will get all the array's members. I think what you actually want is to go throuigh a number of such entries. Then your code would work.
    – Pekka
    Jan 13, 2010 at 17:14

3 Answers 3

19

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().

1
  • 1
    Thanks Pekka! Not only did that solve the problem but it illuminated other problems in the code where I was acting on incorrect assumptions. Much obliged. Jan 13, 2010 at 17:19
1

you dont need the foreach

$info = array( 
                  $entries[ 'title' ], 
                  $entries[ 'gd:when attr' ][ 'startTime' ], 
                  $entries[ 'gd:where attr' ][ 'valueString' ], 
                  $entries[ 'content' ] 
                 );
1
  • Won't I need it if the calendar happens to have more than 1 entry? Jan 13, 2010 at 17:15
0

To overcome this error at first check whether its an array or net then do array elements manipulat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.