[英]How to get values from complex object in php
我正在使用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->
我能以某種方式這樣做嗎?
$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'}
$value
將包含每個項目,您可以使用以下內容
foreach ($calendarList['items'] as $key => $value) {
$value = (object) $value;
$ids[] = $value->id;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.