1

I am working with the Google Calendar API, trying to get the ids from the response object.

Here is the object.

Google_Service_Calendar_CalendarList Object
(
[collection_key:protected] => items
[internal_gapi_mappings:protected] => Array
    (
    )

[etag] => "1454629096373000"
[itemsType:protected] => Google_Service_Calendar_CalendarListEntry
[itemsDataType:protected] => array
[kind] => calendar#calendarList
[nextPageToken] => 
[nextSyncToken] => CIj2xtSj38oCEj1nY2FsL....
[modelData:protected] => Array
    (
        [items] => Array
            (
                [0] => Array
                    (
                        [kind] => calendar#calendarListEntry
                        [etag] => "145461934381000"
                        [id] => tbcv49j3eg@group.calendar.google.com
                        [summary] => Appointments
                        [timeZone] => America/Chicago
                        [colorId] => 24
                        [backgroundColor] => #a47ae2
                        [foregroundColor] => #000000
                        [selected] => 1
                        [accessRole] => reader
                        [defaultReminders] => Array
                            (
                            )

                    )

                [1] => Array
                    (
                        [kind] => calendar#calendarListEntry
                        [etag] => "14546290978873000"
                        [id] => o6lkfgdovcv2r8@group.calendar.google.com
                        [summary] => Test Calendar
                        [timeZone] => America/Chicago
                        [colorId] => 14
                        [backgroundColor] => #9fe1e7
                        [foregroundColor] => #000000
                        [selected] => 1
                        [accessRole] => reader
                        [defaultReminders] => Array
                            (
                            )

                    )

            )

    )

So I did this to get them into an array.

foreach ($calendarList['items'] as $key => $value) {
     $ids[] = $calendarList['items'][$key]['id'];
}

But I would like to reference them in a more Object oriented style like

$calendarList->items->

Can I do this somehow?

skribe
  • 3,595
  • 4
  • 25
  • 36

2 Answers2

3
$calendarListNew=new stdClass();
$calendarListNew->items=new stdClass();

$i=1;
foreach ($calendarList['items'] as $key => $value) {
     $calendarListNew->items->$i = $calendarList['items'][$key]['id'];
     $i++;
}

So you can fetch the values like :

$calendarListNew->items->{'1'}
joHN
  • 1,765
  • 3
  • 15
  • 31
0

$value will have the each items, u can use the following

foreach ($calendarList['items'] as $key => $value) {
    $value = (object) $value;
    $ids[] = $value->id;
}

How to convert an array into an object

Community
  • 1
  • 1
Vijay Arun
  • 414
  • 8
  • 15
  • which u want to be object – Vijay Arun Feb 05 '16 at 04:45
  • That is an improvement since it is more concise. But not really what I am looking for. I can get an id if I use `$calendarList['items'][0]['id']` but I am looking for an object oriented way to the "id – skribe Feb 05 '16 at 04:47