【问题标题】:Why does this PHP array keep acting as if this key is set when it is empty? [duplicate]为什么这个 PHP 数组在它为空时一直表现得好像这个键被设置了? [复制]
【发布时间】:2019-11-24 01:26:03
【问题描述】:

我正在我正在处理的网站上创建一个 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

注意:未定义的偏移量:2 英寸 /home/user/mysite.com/components/data/icsparse.php 上线 193

注意:未定义的偏移量:3 英寸 /home/user/mysite.com/components/data/icsparse.php 上线 194

注意:未定义的偏移量:1 英寸 /home/user/mysite.com/components/data/icsparse.php 上线 198

ics calendar file 以下是ics 文件的前几个条目:

开始:事件

DTSTART:20191123T013000Z

DTEND:20191123T023000Z

DTSTAMP:20191122T205609Z

UID:7mg6p2bj83hadlununj7d9ov40@google.com

创建:20191122T064508Z

描述:

最后修改:20191122T064508Z

位置:Alaska Pacific University\, 4101 University Dr\, Anchorage\, AK 99 508\,美国

序列:0

状态:已确认

总结:篮球 vs Birchwood Christian

传输:不透明

结束:事件

开始:事件

DTSTART;VALUE=DATE:20190624

DTEND;VALUE=DATE:20190629

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

DTSTAMP:20191122T205609Z

UID:002fe82nhfs3qhfn8i84qe2dm5@google.com

创建:20181218T234159Z

描述:

最后修改:20191122T060518Z

地点:

序列:1

状态:已确认

总结:足球训练营(PreK-6th)

传输:不透明

结束:事件

开始:事件

DTSTART;VALUE=DATE:20190617

DTEND;VALUE=DATE: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) 为前几个条目返回的内容:

数组 ( '开始' => '事件 ', 'DTSTART' => '20191123T013000Z ', 'DTEND' => '20191123T023000Z ', 'DTSTAMP' => '20191124T081354Z ', 'UID' => '7mg6p2bj83hadlununj7d9ov40@google.com ', '已创建' => '20191122T064508Z ', '描述' => ' ', '最后修改' => '20191122T064508Z ', '位置' => '阿拉斯加太平洋大学\,4101大学博士\,安克雷奇\,AK 99 ', '序列' => '0 ', '状态' => '已确认 ', 'SUMMARY' => '篮球 vs Birchwood Christian ', 'TRANSP' => '不透明 ', '结束' => '事件 ', )阿拉斯加太平洋大学4101大学博士安克雷奇AK99 AK4101+大学+Drarray ( '开始' => '事件 ', 'DTSTART;VALUE=DATE' => '20190624 ', 'DTEND;VALUE=DATE' => '20190629 ', 'RRULE' => 'FREQ=YEARLY;WKST=SU;COUNT=1;INTERVAL=1 ', 'DTSTAMP' => '20191124T081354Z ', 'UID' => '002fe82nhfs3qhfn8i84qe2dm5@google.com ', '已创建' => '20181218T234159Z ', '描述' => ' ', '最后修改' => '20191122T060518Z ', '位置' => ' ', '序列' => '1 ', '状态' => '已确认 ', 'SUMMARY' => '足球训练营(PreK-6th) ', 'TRANSP' => '不透明 ', '结束' => '事件 ', )

:未定义的偏移量: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' => ' ',
'最后修改' => '20191122T060517Z', '位置' => ' ',
'序列' => '1','状态' => '确认','摘要' => 'STEM Camp (K-8th) ', 'TRANSP' => 'OPAQUE ', 'END' => 'VEVENT ', )

为什么要这样做?

【问题讨论】:

  • print_r($parts)explode 之后,你可能会发现它只有一个元素。
  • 这不是 PHP 的行为,好像 key 已设置。只是你没有做足够的检查。
  • 您是要转义 explode() 的分隔符参数中的逗号,还是输入字符串中的斜线。您的爆炸未能创建预期数量的元素。您的问题是题外话,因为我们没有足够的信息来复制您的问题。
  • @ThanhTrung 在我测试时,我在第 190 行有 echo "SET";,它在每个 $icsEvent 上都返回 SET。这告诉我第 189 行一定是不正确的。我不能使用array_key_exists,因为即使没有输入位置,数组键也始终存在。我应该使用isset 吗?如果是这样,我该如何正确地使用isset 进行检查?
  • 我不打算回答这个问题。太便宜。使用if(trim($eventLoc) != ""){

标签: php arrays parsing icalendar


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多