PHP usort() 是否适用于 DOM 节点列表?

发布于 2024-12-24 02:26:09 字数 1649 浏览 2 评论 0原文

我在 XML 文件中存储了一堆数据,我用 PHP 将其打印为列表。我希望用户能够选择如何对列表进行排序。

我用 usort() 进行了一些实验,但它似乎不起作用。它不会抛出异常,所以我认为这是可行的,但是我的排序功能有问题。我想做的第一件事是让数据按创建日期排序 - 这存储在这样的属性中:

<root>
    <info created="2011-12-31">
        <sometags />
    </info>
    <info created="2012-01-02">
        <sometags />
    </info>
    <info created="2012-01-01">
        <sometags />
    </info>
</root>

我的排序是这样完成的:

function sortByDate($a, $b) {

    //get array of year, month and day
    $adates = explode('-', $a->getAttribute('created'));
    $bdates = explode('-', $b->getAttribute('created'));

    //if the years are not the same, use them to sort
    if ($adates[0] != $bdates[0]) {
        return (intval($adates[0]) < intval($bdates[0])) ? -1 : 1;
    }
    //if the years are the same, try sorting by the months
    else if ($adates[1] != $bdates[1]) {
        return (intval($adates[1]) < intval($bdates[1])) ? -1 : 1;
    }
    //if the years and months are both the same, try sorting by days
    else {
        return (intval($adates[2]) < intval($bdates[2])) ? -1 : 1;
    }

    //if we get this far, the dates are identical
    return 0;
}

这就是我如何称呼它:

$xmlDoc = new DOMDocument();
$xmlDoc->load('data.xml');

$infotags = $xmlDoc->documentElement->getElementsByTagName('info');

usort($infotags, 'sortByDate');

这是一些愚蠢的错误吗我已经做了,或者我应该完全做别的事情吗?

顺便说一句,我知道上面的 if...else 构造实际上不会按正确的顺序对日期进行排序。我只是想让它做一些事情 - 目前 usort() 只是按照开始时的顺序保留节点列表。

I have a bunch of data stored in an XML file that I print out as a list with PHP. I want the user to be able to choose how to sort the list.

I experimented a bit with usort(), but it doesn't seem to be working. It doesn't throw an exception, so I'm thinking that it is doable, but there's something amiss with my sorting function. The first thing I want to do is let the data be sorted by the date it was created - this is stored in an attribute like this:

<root>
    <info created="2011-12-31">
        <sometags />
    </info>
    <info created="2012-01-02">
        <sometags />
    </info>
    <info created="2012-01-01">
        <sometags />
    </info>
</root>

and my sorting is done with this:

function sortByDate($a, $b) {

    //get array of year, month and day
    $adates = explode('-', $a->getAttribute('created'));
    $bdates = explode('-', $b->getAttribute('created'));

    //if the years are not the same, use them to sort
    if ($adates[0] != $bdates[0]) {
        return (intval($adates[0]) < intval($bdates[0])) ? -1 : 1;
    }
    //if the years are the same, try sorting by the months
    else if ($adates[1] != $bdates[1]) {
        return (intval($adates[1]) < intval($bdates[1])) ? -1 : 1;
    }
    //if the years and months are both the same, try sorting by days
    else {
        return (intval($adates[2]) < intval($bdates[2])) ? -1 : 1;
    }

    //if we get this far, the dates are identical
    return 0;
}

and this is how I call it:

$xmlDoc = new DOMDocument();
$xmlDoc->load('data.xml');

$infotags = $xmlDoc->documentElement->getElementsByTagName('info');

usort($infotags, 'sortByDate');

Is it some silly error I've made, or should I be doing something else entirely?

By the way, I'm aware that the if... else construct above won't actually sort the dates in the proper order. I'm just trying to get it to do something - at the moment usort() just leaves the nodelist in the same order it was to begin with.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉薄对峙 2024-12-31 02:26:09

getElementsByTagName 返回一个 DOMNodeList,它是一个迭代器,而不是真正的数组。因此,您无法更改列表的顺序。

首先尝试将其转换为数组。

getElementsByTagName returns a DOMNodeList, which is an Iterator and not a real array. Therefore, you can not change the order of the list.

Try converting it to an array first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文