开发者

How to sort xpath using php

开发者 https://www.devze.com 2023-04-09 03:06 出处:网络
i searched through the website and couldnt find exact result for my question. i need to search through the xml file and sort the output by the 开发者_如何学JAVAorder in date descending.

i searched through the website and couldnt find exact result for my question.

i need to search through the xml file and sort the output by the 开发者_如何学JAVAorder in date descending.

<?xml version="1.0"?>
<entries>
  <entry>
    <date>1299565881</date>
    <action>made an action under category</action>
    <user>Admin</user>
  </entry>
  <entry>
    <date>1299566115</date>
    <action>Item deleted</action>
    <user>Admin</user>
  </entry>
</entries>

here is my code

<?php

$data = simplexml_load_file($filename);

if($to_date_int>$global_date)
    $op = $data->xpath('/entries/entry[date<='.$to_date_int.']');
else
    $op = $data->xpath('/entries/entry');

Thanks in advance..


Since $op will be an array of the matching entry elements, you could use PHP's array sorting functions to sort by their associated date elements.

function sort_entries_by_date($a, $b) {
    return (int) $b->date - (int) $a->date;
}
usort($op, 'sort_entries_by_date');

(If you're using PHP 5.3, the named function could be placed by an inline anonymous function.)

0

精彩评论

暂无评论...
验证码 换一张
取 消