Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用php读取xml文件目录_Php_Xml_Directory - Fatal编程技术网

用php读取xml文件目录

用php读取xml文件目录,php,xml,directory,Php,Xml,Directory,我有一段代码,可以找到目录中的每个xml文件,然后获取日期。但是,我希望它能够响应目录中每个xml文件中的每一行。 xml文件如下所示 <event> <day>11</day> <month>01</month> <year>2013</year> <link>link</link> <title>Title</title> <descripti

我有一段代码,可以找到目录中的每个xml文件,然后获取日期。但是,我希望它能够响应目录中每个xml文件中的每一行。 xml文件如下所示

<event> 
<day>11</day> 
<month>01</month> 
<year>2013</year> 
<link>link</link> 
<title>Title</title> 
<description></description>
</event>
}

例如,对于找到的每个文件,它都会打印出如下内容

 <div class="event"><p>Date: month/day/year</p><p>Title: the xml title</p><p>description: the xml description</p></div>
日期:月/日/年

标题:xml标题

描述:xml描述


作为一个简单的解决方案,如果您想将事件作为循环的一部分进行回送,可以执行以下操作:

$events=array();
//打开此目录中的每个xml文件
foreach(glob(“*.xml”)作为$filename){
//获取当前文件的内容
$xml\u file=file\u get\u contents($filename,file\u TEXT);
//从当前文件的内容创建simplexml对象
$xml=simplexml\u load\u字符串($xml\u文件);
//创建日期、链接、描述等。
$eventDay=(int)$xml->day;
$eventMonth=(int)$xml->month;
$eventYear=(int)$xml->year;
$eventLink=(字符串)$xml->link;
$eventDesc=(字符串)$xml->description;
$eventTitle=(字符串)$xml->title;
如果($eventMonth==$month&&$eventYear==$year)
$events[$eventDay]=数组($eventLink,'linked-day');
回声“
日期:{$eventMonth}/{$eventDay}/{$eventYear}

标题:{$eventTitle}

说明:{$eventDesc}

"; } 返回$events;

唯一增加的是
$eventTitle
echo
部分

非常感谢约翰。哇,这很简单(我现在觉得有点傻lol),所以它可以工作,但现在我不能让它按xml文件中的日期排序,它是按文件创建日期排序的。我该如何让它在xml文件本身中按日期排序呢?@KarenBemusOsh很高兴它有帮助!如果你的问题得到了回答,你可以。要按日期排序,您必须首先将所有事件收集到一个数组中(类似于您拥有的
$events
数组),然后才能对它们进行排序和显示。
 <div class="event"><p>Date: month/day/year</p><p>Title: the xml title</p><p>description: the xml description</p></div>