好的,所以我昨天关于将XML结果排序成关联数组的问题,这很好。 (见我的代码)
<?php
$xmlData = 'http://xml.betfred.com/Horse-Racing-Daily.xml';
$xml = simplexml_load_file($xmlData);
$curdate = date('d/m/Y');
$new_array = array();
$limit = 5;
$c = 0;
foreach ($xml->event as $event) {
if ($limit == $c) {
break;
}
$c++;
$eventd = substr($event->attributes()->{'date'},6,2);
$eventm = substr($event->attributes()->{'date'},4,2);
$eventy = substr($event->attributes()->{'date'},0,4);
$eventt = $event->attributes()->{'time'};
$new_array[$eventy.$eventm.$eventd.$eventt] = array(
'eventd' => $eventd,
'eventm' => $eventm,
'eventy' => $eventy,
'eventt' => $eventt,
'eventdate' => $eventd.'/'.$eventm.'/'.$eventy,
'eventid' => $event->attributes()->{'eventid'},
'eventtime' => $event->attributes()->{'time'},
'eventname' => $event->attributes()->{'name'},
'venue' => $event->attributes()->venue,
);
}
ksort($new_array);
foreach ($new_array as $event_time => $event_data) {
echo "<a href=\"event/".$event_data['eventname']."/".$event_data['eventid']."\">".$event_data['venue'].' - '.$event_data['eventtime'].' - '.$event_data['eventname']."</a><br />".$event_data['eventdate']."<br />";
}
?>
我现在的问题是xml Feed中有记录有空值 所以我现在需要以某种方式运行过滤器,以便如果日期属性值为空或小于2016,则不会显示,结果也必须按$ event_data [&#39; eventtime&#39;]值进行排序
提前致谢
已修复
<?php
$new_array = array();
//$limit = 5;
//$c = 0;
foreach ($xml->event as $event) {
//if ($limit == $c) {
// break;
//}
$checkdate = date('Ymd');
if($event->attributes()->{'date'} == $checkdate) {
$eventd = substr($event->attributes()->{'date'},6,2);
$eventm = substr($event->attributes()->{'date'},4,2);
$eventy = substr($event->attributes()->{'date'},0,4);
$startdate = substr($event->attributes()->{'date'},0,8);
$eventt = $event->attributes()->{'time'};
$new_array[$startdate.$eventt] = array(
'startdate' => $startdate,
'eventt' => $eventt,
'eventd' => $eventd,
'eventm' => $eventm,
'eventy' => $eventy,
'eventdate' => $eventd.'/'.$eventm.'/'.$eventy,
'eventid' => $event->attributes()->{'eventid'},
'eventtime' => $event->attributes()->{'time'},
'eventname' => $event->attributes()->{'name'},
'venue' => $event->attributes()->venue,
);
}
}
ksort($new_array);
$showl = 10;
$n = 0;
foreach ($new_array as $event_time => $event_data) {
if($showl ==$n){ break;}$n++;
$replace = array("/"," ");
$eventname = str_replace($replace,'-', $event_data['eventname']);
$venue = str_replace($replace,'-', $event_data['venue']);
echo "<a href=\"event/".$venue."/".$eventname."/".$event_data['eventid']."\">".$event_data['venue'].' - '.$event_data['eventtime'].' - '.$event_data['eventname']."</a><br />";
}
}
?>
多数民众赞成我想做的事情,但可以想到我的思绪如何空白
答案 0 :(得分:0)
<?php
function GHList5() {
$xmlData = 'http://xml.betfred.com/Greyhounds.xml';
// $xmlData = 'http://xml.betfred.com/Horse-Racing-Daily.xml';
$xml = simplexml_load_file($xmlData);
$curdate = date('d/m/Y');
$new_array = array();
//$limit = 5;
//$c = 0;
foreach ($xml->event as $event) {
// if ($limit == $c) {
// break;
// }
$checkdate = date('Ymd');
if($event->attributes()->{'date'} == $checkdate) {
$eventd = substr($event->attributes()->{'date'},6,2);
$eventm = substr($event->attributes()->{'date'},4,2);
$eventy = substr($event->attributes()->{'date'},0,4);
$startdate = substr($event->attributes()->{'date'},0,8);
$eventt = $event->attributes()->{'time'};
$new_array[$startdate.$eventt] = array(
'startdate' => $startdate,
'eventt' => $eventt,
'eventd' => $eventd,
'eventm' => $eventm,
'eventy' => $eventy,
'eventdate' => $eventd.'/'.$eventm.'/'.$eventy,
'eventid' => $event->attributes()->{'eventid'},
'eventtime' => $event->attributes()->{'time'},
'eventname' => $event->attributes()->{'name'},
'venue' => $event->attributes()->venue,
);
}
}
ksort($new_array);
$showl = 10;
$n = 0;
foreach ($new_array as $event_time => $event_data) {
if($showl ==$n){ break;}$n++;
$replace = array("/"," ");
$eventname = str_replace($replace,'-', $event_data['eventname']);
$venue = str_replace($replace,'-', $event_data['venue']);
echo "<a href=\"event/".$venue."/".$eventname."/".$event_data['eventid']."\">".$event_data['venue'].' - '.$event_data['eventtime'].' - '.$event_data['eventname']."</a><br />";
}
}
?>
答案 1 :(得分:0)
如果使用DOM,则可以使用PHP函数使用Xpath过滤节点。
function isNewerThen($current, $minimum) {
return $current >= $minimum;
}
$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$xpath->registerNamespace("p", "http://php.net/xpath");
$xpath->registerPhpFunctions('isNewerThen');
$expression = '/category/event[
p:function(
"isNewerThen",
concat(@date, "T", @time, "Z"),
"20160106T1600Z"
)
]';
$events = iterator_to_array(
$xpath->evaluate($expression)
);
concat()
是一个Xpath函数,它将所有参数粘合在一起作为字符串。参数可以是节点,节点列表,标量或文字。如果它是节点列表,则第一个节点将被强制转换为字符串。
p:function
已解析为{http://php.net/xpath}:function
并允许您从Xpath调用PHP函数。第一个参数是函数名称。所有其他参数都传递给调用。在这种情况下,属性可以用作ISO 8601日期时间值的一部分。
iterator_to_array
将返回的DOMNodeList
转换为数组。
数组元素仍然是DOM节点。您可以使用Xpath或使用DOM Api方法获取属性并进行比较。 usort
允许您使用比较函数对数组进行排序。
usort(
$events,
function($nodeA, $nodeB) {
$timeA = sprintf('%sT%sZ', $nodeA->getAttribute('date'), $nodeA->getAttribute('time'));
$timeB = sprintf('%sT%sZ', $nodeB->getAttribute('date'), $nodeB->getAttribute('time'));
return strtotime($timeA) - strtotime($timeB);
}
);
foreach ($events as $event) {
echo $xpath->evaluate('string(@meeting)', $event), " ";
echo $event->getAttribute('date'), " ";
echo $event->getAttribute('time'), "\n";
}
Portman Park 20160106 1600
Marseille V' 20160106 1605
Kempton 20160106 1610
Steepledowns 20160106 1620
...