我在弄清楚如何循环使用Google Calendar iCal创建的数组时遇到了麻烦。我不知道自己是否在寻找正确的东西,因为当我搜索“ php循环ical数组”以及其他类似的变体时,似乎找不到该问题的任何答案。我想使用一个foreach循环,但它一直包含Google日历的标题。这是我不断得到的结果
-//Google Inc//Google Calendar 70.9054//EN
2.0
GREGORIAN
PUBLISH
Faith Lutheran Church
America/Anchorage
Faith Lutheran Church in Anchorage, Alaska is a congregation of Bible-believing Lutheran Christians. Please join us for worship on Sunday mornings at 10:30 AM. Faith offers quality Christian elementary school (K-8) and preschool (3 & 4 yr olds) education. We are a proud member of the Wisconsin Evangelical Lutheran Synod.
Array
Array
使用时
$file1 = "https://calendar.google.com/calendar/ical/i8j5d94tpgnnqu6h8q3mt6uc48%40group.calendar.google.com/public/basic.ics";
include('../assets/includes/iCalEasyReader.php');
$ical = new iCalEasyReader();
$lines = $ical->load(file_get_contents($file1));
foreach($lines as $event){
echo $event."<br>";
}
这是iCalEasyReader.php:
<?php
/**
* iCalEasyReader is an easy to understood class, loads a "ics" format string and returns an array with the traditional iCal fields
*
* @category Parser
* @author Matias Perrone <matias.perrone at gmail dot com>
* @author Timo Henke <phpstuff@thenke.de> (Some ideas taken partially from iCalParse on http://www.phpclasses.org/)
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @version 1.4.1
* @param string $data ics file string content
* @param array|false $data $makeItEasy the idea is to convert this "keys" into the "values", converting the DATE and DATE-TIME values to the respective DateTime type of PHP, also all the keys are lowercased
* @return array|false
*/
class iCalEasyReader
{
private $ical = null;
private $_lastitem = null;
public function &load($data)
{
$this->ical = false;
$regex_opt = 'mib';
// Lines in the string
$lines = mb_split( '[\r\n]+', $data );
// Delete empty ones
$last = count( $lines );
for($i = 0; $i < $last; $i ++)
{
if (trim( $lines[$i] ) == '')
unset( $lines[$i] );
}
$lines = array_values( $lines );
// First and last items
$first = 0;
$last = count( $lines ) - 1;
if (! ( mb_ereg_match( '^BEGIN:VCALENDAR', $lines[$first], $regex_opt ) and mb_ereg_match( '^END:VCALENDAR', $lines[$last], $regex_opt ) ))
{
$first = null;
$last = null;
foreach ( $lines as $i => &$line )
{
if (mb_ereg_match( '^BEGIN:VCALENDAR', $line, $regex_opt ))
$first = $i;
if (mb_ereg_match( '^END:VCALENDAR', $line, $regex_opt ))
{
$last = $i;
break;
}
}
}
// Procesing
if (! is_null( $first ) and ! is_null( $last ))
{
$lines = array_slice( $lines, $first + 1, ( $last - $first - 1 ), true );
$group = null;
$parentgroup = null;
$this->ical = [];
$addTo = [];
$addToElement = null;
foreach ( $lines as $line )
{
$clave = null;
$pattern = '^(BEGIN|END)\:(.+)$'; // (VALARM|VTODO|VJOURNAL|VEVENT|VFREEBUSY|VCALENDAR|DAYLIGHT|VTIMEZONE|STANDARD)
mb_ereg_search_init( $line );
$regs = mb_ereg_search_regs( $pattern, $regex_opt );
if ($regs)
{
// $regs
// 0 => BEGIN:VEVENT
// 1 => BEGIN
// 2 => VEVENT
switch ( $regs[1] )
{
case 'BEGIN' :
if (! is_null( $group ))
$parentgroup = $group;
$group = trim( $regs[2] );
// Adding new values to groups
if (is_null( $parentgroup ))
{
if (! array_key_exists( $group, $this->ical ))
$this->ical[$group] = [null];
else
$this->ical[$group][] = null;
}
else
{
if (! array_key_exists( $parentgroup, $this->ical ))
$this->ical[$parentgroup] = [$group => [null]];
if (! array_key_exists( $group, $this->ical[$parentgroup] ))
$this->ical[$parentgroup][$group] = [null];
else
$this->ical[$parentgroup][$group][] = null;
}
break;
case 'END' :
if (is_null( $group ))
$parentgroup = null;
$group = null;
break;
}
continue;
}
if (! in_array( $line[0], [" ", "\t"] ))
$this->addItem( $line, $group, $parentgroup );
else
$this->concatItem( $line );
}
}
return $this->ical;
}
public function addType(&$value, $item)
{
$type = explode( '=', $item );
if (count( $type ) > 1 and $type[0] == 'VALUE')
$value['type'] = $type[1];
else
$value[$type[0]] = $type[1];
return $value;
}
public function addItem($line, $group, $parentgroup)
{
$line = $this->transformLine( $line );
$item = explode( ':', $line, 2 );
// If $group is null is an independent value
if (is_null( $group ))
{
$this->ical[$item[0]] = ( count( $item ) > 1 ? $item[1] : null );
$this->_lastitem = &$this->ical[$item[0]];
}
// If $group is set then is an item of a group
else
{
$subitem = explode( ';', $item[0], 2 );
if (count( $subitem ) == 1)
$value = ( count( $item ) > 1 ? $item[1] : null );
else
{
$value = ['value' => $item[1]];
$this->addType( $value, $subitem[1] );
}
// Multi value
if (is_string( $value ))
{
$z = explode( ';', $value );
if (count( $z ) > 1)
{
$value = [];
foreach ( $z as &$v )
{
$t = explode( '=', $v );
$value[$t[0]] = $t[count( $t ) - 1];
}
}
unset( $z );
}
if (is_null( $parentgroup ))
{
$this->ical[$group][count( $this->ical[$group] ) - 1][$subitem[0]] = $value;
$this->_lastitem = &$this->ical[$group][count( $this->ical[$group] ) - 1][$subitem[0]];
}
else
{
$this->ical[$parentgroup][$group][count( $this->ical[$parentgroup][$group] ) - 1][$subitem[0]] = $value;
$this->_lastitem = &$this->ical[$parentgroup][$group][count( $this->ical[$parentgroup][$group] ) - 1][$subitem[0]];
}
}
}
public function concatItem($line)
{
$line = mb_substr( $line, 1 );
if (is_array( $this->_lastitem ))
{
$line = $this->transformLine( $this->_lastitem['value'] . $line );
$this->_lastitem['value'] = $line;
}
else
{
$line = $this->transformLine( $this->_lastitem . $line );
$this->_lastitem = $line;
}
}
public function transformLine($line)
{
$patterns = ['\\\\[n]', '\\\\[t]', '\\\\,', '\\\\;'];
$replacements = ["\n", "\t", ",", ";"];
return $this->mb_eregi_replace_all( $patterns, $replacements, $line );
}
public function mb_eregi_replace_all($pattern, $replacement, $string)
{
if (is_array( $pattern ) and is_array( $replacement ))
{
foreach ( $pattern as $i => $patron )
{
if (array_key_exists( $i, $replacement ))
$reemplazo = $replacement[$i];
else
$reemplazo = '';
$string = mb_eregi_replace( $patron, $reemplazo, $string );
}
}
elseif (is_string( $pattern ) and is_string( $replacement ))
$string = mb_eregi_replace( $pattern, $replacement, $string );
return $string;
}
}
我想遍历下面所示数组的VEVENT条目。
BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:Faith Lutheran Church
X-WR-TIMEZONE:America/Anchorage
X-WR-CALDESC:Faith Lutheran Church in Anchorage\, Alaska is a congregation
of Bible-believing Lutheran Christians. Please join us for worship on Sunda
y mornings at 10:30 AM. Faith offers quality Christian elementary school (K
-8) and preschool (3 & 4 yr olds) education. We are a proud member of the W
isconsin Evangelical Lutheran Synod.
BEGIN:VTIMEZONE
TZID:America/Anchorage
X-LIC-LOCATION:America/Anchorage
BEGIN:DAYLIGHT
TZOFFSETFROM:-0900
TZOFFSETTO:-0800
TZNAME:AKDT
DTSTART:19700308T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:-0800
TZOFFSETTO:-0900
TZNAME:AKST
DTSTART:19701101T020000
RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DTSTART:20190313T020000Z
DTEND:20190313T030000Z
DTSTAMP:20190303T061701Z
UID:2be6h1ru67cg0os886g0dqgjc5@google.com
CREATED:20190228T205014Z
DESCRIPTION:
LAST-MODIFIED:20190228T205015Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Tuesday Teen Night
TRANSP:OPAQUE
END:VEVENT
BEGIN:VEVENT
DTSTART:20190303T203000Z
DTEND:20190303T210000Z
DTSTAMP:20190303T061701Z
UID:1kj37lnrqcbefunj0mmsktajsf@google.com
CREATED:20190227T173557Z
DESCRIPTION:
LAST-MODIFIED:20190227T173557Z
LOCATION:Faith Lutheran Church\, 5200 Lake Otis Pkwy\, Anchorage\, AK 99507
\, USA
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Congregation Call Meeting
TRANSP:OPAQUE
END:VEVENT
BEGIN:VEVENT
DTSTART:20190316T230000Z
DTEND:20190317T010000Z
DTSTAMP:20190303T061701Z
UID:0bl4hkd2ogjplurldf59mihpen@google.com
CREATED:20190227T173218Z
DESCRIPTION:
LAST-MODIFIED:20190227T173218Z
LOCATION:Faith Lutheran Church\, 5200 Lake Otis Pkwy\, Anchorage\, AK 99507
\, USA
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Trivia Contest Fellowship Event
TRANSP:OPAQUE
END:VEVENT
我对数组不是很熟悉,这就是为什么我被困住了。我查看了包括w3schools.com在内的一些有关php数组的教程,但我只是不明白为什么它不断返回标头而不是事件。
答案 0 :(得分:0)
好吧,在尝试了几种不同的方法之后,我找到了解决方案。您需要通过在基本数组变量之后的方括号中使用子数组名称,在foreach语句中设置子数组,如下所示:
$xlnm #_filterdatabase
我希望这可以帮助任何寻求解决方案的人。