简体   繁体   中英

How to get values from complex object in php

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?

$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'}

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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