我試圖導入事件到我的谷歌日曆帳戶,但是我的代碼無法導入由於錯誤:Cannot use object of type Event as array
這個出現在我的腳本就設在這裏行2149:現在無法使用類型事件的對象數組
$exported_events = array();
foreach($events as $event_id => $event)
{
$event = new Event();
$event->setSummary($event->location);
$event->setLocation($event->location);
$start = new EventDateTime();
$start->setDateTime($event['starts']); // Line 2149
$event->setStart($start);
$end = new EventDateTime();
$end->setDateTime((object) $event['ends']);
$event->setEnd($end);
$attendees = array();
foreach($event['people'] as $i => $person)
{
$attendee = new EventAttendee();
$attendee->setEmail($person->email);
$attendees[] = $attendee;
$event->attendees = $attendees;
}
$createdEvent = $service->events->insert('primary', $event);
$calendar_event_id = $createdEvent->getId();
if(!is_null($calendar_event_id))
{
$exported_events[] = $event_id;
}
}
,如我的foreach
循環走過的$events
陣列,它引發了錯誤與$event['location']
所以我改成$event->location
,現在它拋出上線2149的誤差,其是一個數組項(該陣列被印刷下面的頁面),如果我改變它$event->starts
它會拋出我這個錯誤:Undefined property: Event::$starts
即使我把(object) $event['starts']
我噸仍然不會工作。
我怎樣才能讓它工作? var_dump($event['starts'])
以字符串形式返回。
爲每次$event
數組:
Array
(
[location] => Some location in the US
[starts] => 2012-03-13T10:00:00
[ends] => 2012-03-13T14:00:00
[people] => Array
(
[0] => Array
(
[name] => Joan Uton
[email] => [email protected]
)
[1] => Array
(
[name] => Jack Sparrow
[email] => [email protected]
)
[2] => Array
(
[name] => Barack Obama
[email] => [email protected]
)
)
)
'的foreach($事件$事項標識=> $事件){ $事件=新的事件();' - 這就是你與對象覆蓋陣列。 – Niko 2012-03-09 23:48:19
Niko是正確的。使用變量作爲對象和數組本身並不總是會導致錯誤(ArrayAccess接口)。這是OP覆蓋變量的事實。 – webbiedave 2012-03-09 23:51:59
修正報價澄清 – marfis 2012-03-09 23:59:58