简体   繁体   English

如何在php中从复杂对象中获取值

[英]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. 我正在使用Google Calendar API,尝试从响应对象中获取ID。

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 $value将包含每个项目,您可以使用以下内容

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

How to convert an array into an object 如何将数组转换为对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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