我正在尝试将一个事件导入我的Google日历帐户,但由于错误导致我的代码无法导入: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] => rnewkj@hotmail.co.uk
)
[1] => Array
(
[name] => Jack Sparrow
[email] => potc@gmail.com
)
[2] => Array
(
[name] => Barack Obama
[email] => barak@obama.com
)
)
)
答案 0 :(得分:3)
看起来你正在使用两种类型的$ event。一个作为数组,一个作为对象。
foreach($events as $event_id => $event) // $event is array
//...
$event = new Event(); // array is overwritten by new object
通过给一个类型指定一个不同的名称来重构。
答案 1 :(得分:0)
您正在覆盖您的变量。更改其中一个$event
变量的名称:
foreach($events as $event_id => $event)
{
$event = new Event(); // You just overwrote the above variable. Use a different name!