【问题标题】:How to get values from complex object in php如何从php中的复杂对象中获取值
【发布时间】:2016-05-14 23:15:52
【问题描述】:

我正在使用 Google Calendar API,试图从响应对象中获取 ID。

这是对象。

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

                    )

            )

    )

所以我这样做是为了将它们放入一个数组中。

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

但我想以更面向对象的风格引用它们,例如

$calendarList->items->

我可以这样做吗?

【问题讨论】:

标签: php object


【解决方案1】:
$calendarListNew=new stdClass();
$calendarListNew->items=new stdClass();

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

因此您可以获取如下值:

$calendarListNew->items->{'1'}

【讨论】:

  • 这个比较像。但我收到一个错误。 syntax error, unexpected '1' (T_LNUMBER), expecting identifier (T_STRING) or variable (T_VARIABLE)
  • 试试这个 $calendarListNew->items->{'1'}
  • @skribe:如果有帮助,请接受我的回答兄弟。谢谢。美好的一天。
【解决方案2】:

$value会有各项,你可以用下面的

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

How to convert an array into an object

【讨论】:

  • 你想成为对象
  • 这是一个改进,因为它更简洁。但不是我真正想要的。如果我使用$calendarList['items'][0]['id'],我可以获得一个 id,但我正在寻找一种面向对象的方式来获取“id
猜你喜欢
  • 2016-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多