我已经开始使用simplexml函数,它似乎比我尝试使用的上一个其他解析器更好。我已经到了需要对物品进行分类的阶段 通过transmissiondate - 我尝试使用uasort,但没有对项目的顺序进行任何更改。
有时候一个程序在同一天多次出现 - 如果排序更容易,视频ID可以提供任何帮助吗?
这个对象是什么样的:
[0] => SimpleXMLElement Object
(
[VideoID] => 108059
[Genre] => Music
[ProgrammeName] => MTV
[OriginalAiringDate] => 2009-11-10T19:22:24
[TransmissionDate] => 2009-11-10T19:22:24
)
[1] => SimpleXMLElement Object
(
[VideoID] => 108395
[ExpiryDate] => 2009-12-12T23:59:59
[DateCreated] => 2009-11-12T13:28:54
[Genre] => Music
[ProgrammeName] => MTV
[OriginalAiringDate] => 2009-11-12T19:22:32
[TransmissionDate] => 2009-11-12T19:22:32
)
$xml = simplexml_load_file("data.xml");
$count = 0;
$sortItem = 0;
$dateformat = "D j M, g:ia";
$sortArray = array();
foreach($xml->CatchUp as $item){
$sortArray[$count][TransmissionDate] = $item;
if($count < 4){
print "<p>Programme Name:<strong> " . $item->ProgrammeName. "</strong></p>";
print "<p>Date Shown:<strong> " . date($dateformat, strtotime($item->TransmissionDate)). "</strong></p>";
print "<p>Description:<strong> " . trunc($item->ShortSynopsis,30, " ")."</strong></p>";
print "<p><a href='". $item->VideoID. "'>". $item->VideoID."</a></p>";
$count++;
}
}
}
ASORT($ sortArray);
答案 0 :(得分:1)
我看到两种方法。第一个是创建一个包含TransmissionDate
值的数组,然后是另一个包含相应节点的数组,然后使用array_multisort()
。这有点单调乏味,所以这就是我要做的事情:下载SimpleDOM并使用sortedXPath()
include 'SimpleDOM.php';
$xml = simpledom_load_file("data.xml");
$dateformat = "D j M, g:ia";
foreach($xml->sortedXPath('CatchUp[ProgrammeName="MTV"]', 'TransmissionDate') as $i => $item)
{
if ($i == 4)
{
// I assume you only want the first 4
break;
}
print "<p>Programme Name:<strong> " . $item->ProgrammeName. "</strong></p>";
print "<p>Date Shown:<strong> " . date($dateformat, strtotime($item->TransmissionDate)). "</strong></p>";
print "<p>Description:<strong> " . trunc($item->ShortSynopsis,30, " ")."</strong></p>";
print "<p><a href='". $item->VideoID. "'>". $item->VideoID."</a></p>";
}
答案 1 :(得分:0)
从SimpleXMLElement对象中提取数据时尝试使用强制转换
使用视频ID: $sortArray[$count][VideoID] = (int)$item;