首页 > 解决方案 > 为什么这个 PHP 数组在它为空时一直表现得好像这个键被设置了?

问题描述

我正在我正在处理的网站上创建一个 ics 文件解析器,无论我尝试编写什么来检查$icsEvent['LOCATION']索引是否为空,它都会一直表现得好像它有一个值。我正在尝试获取输入到 ics 事件中的地址并将其分解为位置名称、位置街道、位置城市、位置状态和位置 zip。ics 文件中的第一个事件有一个地址,而第二个没有。当它到达第二个事件时,它仍然表现得好像第二个事件不是空的,并导致它为未定义的偏移量抛出错误。

$file = "https://calendar.google.com/calendar/ical/nhfb76hhlt4ts86jmk4n9bsghk%40group.calendar.google.com/public/basic.ics";
/* Getting events from isc file */
$obj = new ics();
$icsEvents = $obj->getIcsEventsAsArray($file);
/* Here we are getting the timezone to get the event dates according to geo location */
$timeZone = trim($icsEvents[1]['X-WR-TIMEZONE']);
unset($icsEvents[1]);
unset($icsEvents[2]);
unset($icsEvents[3]);
unset($icsEvents[4]);
unset($icsEvents[5]);
unset($icsEvents[6]);
foreach($icsEvents as $icsEvent){
  $eventKey = removeEmail($icsEvent['UID']);
  /* Getting start date and time */
  $start = isset($icsEvent['DTSTART;VALUE=DATE']) ? $icsEvent['DTSTART;VALUE=DATE'] : $icsEvent['DTSTART'];
  /* Converting to datetime and apply the timezone to get proper date time */
  $startDt = new DateTime($start);
  $startDt->setTimezone(new DateTimeZone($timeZone));
  $startDate = $startDt->format('Ymd H:i:s');
  $eventDate = $startDt->format('m.d.Y @ g:iA');
  $startSrch = $startDt->format('Ymd');
  /* Getting end date with time */
  $end = isset($icsEvent['DTEND;VALUE=DATE']) ? $icsEvent['DTEND;VALUE=DATE'] : $icsEvent['DTEND'];
  $endDt = new DateTime($end);
  $endDate = $endDt->format('Ymd H:i:s');
  /* Determining if the event is more than one day */
  if($endDt->format('Ymd') != $startDt->format('Ymd')){
    $eventDays = 1;
  }else{
    $eventDays = 0;
  }
  /* Getting the name of event */
  $eventName = $icsEvent['SUMMARY'];
  /* Getting the description of event */
  $eventDesc = $icsEvent['DESCRIPTION'];
  /* Getting the location of event */
  $eventLoc = $icsEvent['LOCATION'];
  if($eventLoc){
    $parts = explode("\, ",$eventLoc);
    $locName = $parts[0];
    $locStreet = $parts[1];
    $locCity = $parts[2];
    $locStateZ = $parts[3];
    $parts = explode(" ",$locStateZ);
    $locState = $parts[0];
    $navState = convertState($locState);
    $locZIP = $parts[1];
  }else{
    $locName = "Any Location Name";
    $locStreet = "5200 Lake Gold Parkway";
    $locCity = "Anytown";
    $locState = "MN";
    $navState = "Minnesota";
    $locZIP = "80134";
  }
  echo $locName;
  echo $locStreet;
  echo $locCity;
  echo $locState;
  echo $locZIP;
  echo $navState;
  $find = " ";
  $replace = "+";
  $navStreet = str_replace($find,$replace,$locStreet);
  echo $navStreet;
  /* Getting the image of event */
  if(array_key_exists("ATTACH",$icsEvent)){
    $eventImg = imageCorrection($icsEvent['ATTACH']);
  }else{
    $eventImg = "/components/img/event-default";
  }
  /* Creating the url of event */
  $eventURL = seoURL($eventName);
  /* Creating the id of event */
  $startSwtch = date('B',strtotime($startDate));
  $eventID = $eventKey."{$startSwtch}";

  $query = "SELECT * FROM calendar WHERE event_key='{$eventKey}' AND start_date LIKE '{$startSrch}%'";
  $update = "UPDATE calendar SET event='".$connection->real_escape_string($eventName)."',url='".$connection->real_escape_string($eventURL)."',image='".$connection->real_escape_string($eventImg)."',description='".$connection->real_escape_string($eventDesc)."',locationName='".$connection->real_escape_string($locName)."',locationStreet='".$connection->real_escape_string($locStreet)."',locationCity='".$connection->real_escape_string($locCity)."',locationState='".$connection->real_escape_string($locState)."',locationZip='".$connection->real_escape_string($locZIP)."',start_date='{$startDate}',end_date='{$endDate}',multiDay='{$eventDays}',church='1' WHERE (event_key='{$eventKey}' AND start_date LIKE '{$start}%')";
  $insert = "INSERT INTO calendar VALUES ('{$eventKey}','".$connection->real_escape_string($eventName)."','".$connection->real_escape_string($eventURL)."','".$connection->real_escape_string($eventImg)."','".$connection->real_escape_string($eventDesc)."','".$connection->real_escape_string($locName)."','".$connection->real_escape_string($locStreet)."','".$connection->real_escape_string($locCity)."','".$connection->real_escape_string($locState)."','".$connection->real_escape_string($locZIP)."','','','{$startDate}','{$endDate}','{$eventDays}','1','0','0','0','0','0','0','0','0','0','0'";

  if($queryDB = $connection->query($query)){
    $row_cnt = $queryDB->num_rows;
    if($row_cnt != 0){
      $updateDB = $connection->query($update);
    }else{
      $insertDB = $connection->query($insert);
    }
  }

注意:未定义的偏移量:1 在 /home/user/mysite.com/components/data/icsparse.php 第 192 行

注意:未定义的偏移量:第 193 行 /home/user/mysite.com/components/data/icsparse.php 中的 2

注意:未定义的偏移量:第 194 行 /home/user/mysite.com/components/data/icsparse.php 中的 3

注意:未定义的偏移量:1 在 /home/user/mysite.com/components/data/icsparse.php 第 198 行

ics 日历文件 以下是该文件的前几个条目ics

开始:事件

DTSTART:20191123T013000Z

DTEND:20191123T023000Z

DTSTAMP:20191122T205609Z

UID:7mg6p2bj83hadlununj7d9ov40@google.com

创建于:20191122T064508Z

描述:

最后修改:20191122T064508Z

地点:阿拉斯加太平洋大学\, 4101 University Dr\, Anchorage\, AK 99 508\, USA

序列:0

状态:已确认

摘要:篮球vs Birchwood Christian

传输:不透明

结束:事件

开始:事件

DTSTART;值=日期:20190624

DTEND;值=日期:20190629

RRULE:FREQ=YEARLY;WKST=SU;COUNT=1;INTERVAL=1

DTSTAMP:20191122T205609Z

UID:002fe82nhfs3qhfn8i84qe2dm5@google.com

创建:20181218T234159Z

描述:

最后修改:20191122T060518Z

地点:

序列:1

状态:已确认

摘要:足球训练营(PreK-6th)

传输:不透明

结束:事件

开始:事件

DTSTART;值=日期:20190617

DTEND;值=日期:20190622

RRULE:FREQ=YEARLY;WKST=SU;COUNT=1;INTERVAL=1

DTSTAMP:20191122T205609Z

UID:4e27isqv5puk2ctc2r2pa6vuc9@google.com

创建:20181218T234147Z

描述:

最后修改:20191122T060517Z

地点:

序列:1

状态:已确认

摘要:STEM 营(K-8th)

传输:不透明

结束:事件

开始:事件

DTSTART:20191124T010000Z

DTEND:20191124T040000Z

DTSTAMP:20191122T205609Z

UID:4gi5mcte11iefak3rhba9l05sk@google.com

创建:20191119T232703Z

描述:

最后修改:20191119T232703Z

地点:

序列:0

状态:已确认

摘要:父母之夜

传输:不透明

结束:事件

以下是var_export($icsEvent)前几个条目返回的内容:

数组('BEGIN' => 'VEVENT', 'DTSTART' => '20191123T013000Z', 'DTEND' => '20191123T023000Z', 'DTSTAMP' => '20191124T081354Z', 'UID' => '7mg6p40bj83hadlununj7d , 'CREATED' => '20191122T064508Z', 'DESCRIPTION' => ' ', 'LAST-MODIFIED' => '20191122T064508Z', 'LOCATION' => '阿拉斯加太平洋大学\, 4101 University Dr\, Anchorage\, AK 99 ', 'SEQUENCE' => '0', 'STATUS' => 'CONFIRMED ', 'SUMMARY' => '篮球 vs Birchwood Christian', 'TRANSP' => 'OPAQUE', 'END' => 'VEVENT',)Alaska Pacific University4101 University DrAnchorageAK99 AK4101+University+Drarray ('BEGIN' => 'VEVENT', 'DTSTART;VALUE=DATE' => '20190624', 'DTEND;VALUE=DATE' => '20190629', 'RRULE' => 'FREQ=YEARLY;WKST=SU;COUNT=1;INTERVAL=1', 'DTSTAMP' => '20191124T081354Z', 'UID' => '002fe82nhfs3qhfn8i84qe2dm5@google.com', '已创建' => '20181218T234159Z' , 'DESCRIPTION' => ' ', 'LAST-MODIFIED' => '20191122T060518Z', 'LOCATION' => ' ', 'SEQUENCE' => '1', '状态' => '确认', 'SUMMARY' = > '足球训练营(PreK-6th)','TRANSP' => '不透明', 'END' => 'VEVENT', )

:未定义的偏移量:1 in ... 在第 193 行

:未定义的偏移量:2 in ... 在第 194 行

:未定义的偏移量:3 in ... 在第 195 行

:未定义的偏移量:1 in ... 在第 199 行

数组('BEGIN' => 'VEVENT', 'DTSTART;VALUE=DATE' => '20190617', 'DTEND;VALUE=DATE' => '20190622', 'RRULE' => 'FREQ=YEARLY;WKST=SU ;COUNT=1;INTERVAL=1 ', 'DTSTAMP' => '20191124T081354Z', 'UID' => '4e27isqv5puk2ctc2r2pa6vuc9@google.com', '已创建' => '20181218T234147Z', 'DESCRIPTION' => ' ',
' LAST-MODIFIED' => '20191122T060517Z', 'LOCATION' => ' ',
'SEQUENCE' => '1', 'STATUS' => 'CONFIRMED', 'SUMMARY' => 'STEM Camp (K-8th)' , 'TRANSP' => '不透明', 'END' => 'VEVENT', )

为什么要这样做?

标签: phparraysparsingicalendar

解决方案


由于缺少部分,您需要在访问索引数组时检查是否存在。

$locStreet  = isset($parts[1]) ? $parts[1] : '';

推荐阅读