usort 返回不可预测的结果

发布于 2024-12-07 18:35:48 字数 4523 浏览 0 评论 0原文

我有一个从数据库返回的对象数组。未排序数组上的 print_r 如下所示:

Array
(
    [0] => stdClass Object
        (
            [nid] => 53162
            [title] => Kroger 'Giving Hope' Standee
            [path] => node/53162
        )

    [1] => stdClass Object
        (
            [nid] => 64185
            [title] => Kroger 'Giving Hope' Stanchion Sign
            [path] => node/64185
        )

    [2] => stdClass Object
        (
            [nid] => 52190
            [title] => Betty Crocker Kroger 'Giving Hope' Shipper
            [path] => node/52190
        )

    [3] => stdClass Object
        (
            [nid] => 53159
            [title] => Frito-Lay Kroger 'Giving Hope' Packaging
            [path] => node/53159
        )

    [4] => stdClass Object
        (
            [nid] => 53164
            [title] => Nabisco Kroger 'Giving Hope' Violator
            [path] => node/53164
        )

    [5] => stdClass Object
        (
            [nid] => 52607
            [title] => Doritos Kroger 'Giving Hope' Packaging
            [path] => node/52607
        )

    [6] => stdClass Object
        (
            [nid] => 52720
            [title] => Kroger Big K Cola 'Giving Hope' Packaging
            [path] => node/52720
        )

    [7] => stdClass Object
        (
            [nid] => 52729
            [title] => Windex Kroger 'Giving Hope' Packaging
            [path] => node/52729
        )

    [8] => stdClass Object
        (
            [nid] => 52731
            [title] => Ziploc Kroger 'Giving Hope' Packaging
            [path] => node/52731
        )

    [9] => stdClass Object
        (
            [nid] => 53157
            [title] => Stacy's Kroger 'Giving Hope' Cut Cases
            [path] => node/53157
        )

)

我想按 nid 属性对数组进行排序,因此我使用了自定义 usort 函数(找到 这里。)

function my_search_sort($a, $b) {
  strcmp($a->nid, $b->nid);
}

我这样称呼它:

usort($docs, 'my_search_sort');

然后做了另一个 print_r。结果看起来像这样:

Array
(
    [0] => stdClass Object
        (
            [nid] => 52720
            [title] => Kroger Big K Cola 'Giving Hope' Packaging
            [path] => node/52720
        )

    [1] => stdClass Object
        (
            [nid] => 52729
            [title] => Windex Kroger 'Giving Hope' Packaging
            [path] => node/52729
        )

    [2] => stdClass Object
        (
            [nid] => 52731
            [title] => Ziploc Kroger 'Giving Hope' Packaging
            [path] => node/52731
        )

    [3] => stdClass Object
        (
            [nid] => 53157
            [title] => Stacy's Kroger 'Giving Hope' Cut Cases
            [path] => node/53157
        )

    [4] => stdClass Object
        (
            [nid] => 52607
            [title] => Doritos Kroger 'Giving Hope' Packaging
            [path] => node/52607
        )

    [5] => stdClass Object
        (
            [nid] => 53164
            [title] => Nabisco Kroger 'Giving Hope' Violator
            [path] => node/53164
        )

    [6] => stdClass Object
        (
            [nid] => 64185
            [title] => Kroger 'Giving Hope' Stanchion Sign
            [path] => node/64185
        )

    [7] => stdClass Object
        (
            [nid] => 52190
            [title] => Betty Crocker Kroger 'Giving Hope' Shipper
            [path] => node/52190
        )

    [8] => stdClass Object
        (
            [nid] => 53159
            [title] => Frito-Lay Kroger 'Giving Hope' Packaging
            [path] => node/53159
        )

    [9] => stdClass Object
        (
            [nid] => 53162
            [title] => Kroger 'Giving Hope' Standee
            [path] => node/53162
        )

)

它显然在做一些事情,但它没有根据 nid 升序排序。我需要做什么才能让它发挥作用?

I have an array of objects returned from a database. A print_r on the unsorted array looks like this:

Array
(
    [0] => stdClass Object
        (
            [nid] => 53162
            [title] => Kroger 'Giving Hope' Standee
            [path] => node/53162
        )

    [1] => stdClass Object
        (
            [nid] => 64185
            [title] => Kroger 'Giving Hope' Stanchion Sign
            [path] => node/64185
        )

    [2] => stdClass Object
        (
            [nid] => 52190
            [title] => Betty Crocker Kroger 'Giving Hope' Shipper
            [path] => node/52190
        )

    [3] => stdClass Object
        (
            [nid] => 53159
            [title] => Frito-Lay Kroger 'Giving Hope' Packaging
            [path] => node/53159
        )

    [4] => stdClass Object
        (
            [nid] => 53164
            [title] => Nabisco Kroger 'Giving Hope' Violator
            [path] => node/53164
        )

    [5] => stdClass Object
        (
            [nid] => 52607
            [title] => Doritos Kroger 'Giving Hope' Packaging
            [path] => node/52607
        )

    [6] => stdClass Object
        (
            [nid] => 52720
            [title] => Kroger Big K Cola 'Giving Hope' Packaging
            [path] => node/52720
        )

    [7] => stdClass Object
        (
            [nid] => 52729
            [title] => Windex Kroger 'Giving Hope' Packaging
            [path] => node/52729
        )

    [8] => stdClass Object
        (
            [nid] => 52731
            [title] => Ziploc Kroger 'Giving Hope' Packaging
            [path] => node/52731
        )

    [9] => stdClass Object
        (
            [nid] => 53157
            [title] => Stacy's Kroger 'Giving Hope' Cut Cases
            [path] => node/53157
        )

)

I want to sort the array by the nid property, so I used a custom usort function (found here.)

function my_search_sort($a, $b) {
  strcmp($a->nid, $b->nid);
}

I called it like this:

usort($docs, 'my_search_sort');

and then did another print_r. The results look like this:

Array
(
    [0] => stdClass Object
        (
            [nid] => 52720
            [title] => Kroger Big K Cola 'Giving Hope' Packaging
            [path] => node/52720
        )

    [1] => stdClass Object
        (
            [nid] => 52729
            [title] => Windex Kroger 'Giving Hope' Packaging
            [path] => node/52729
        )

    [2] => stdClass Object
        (
            [nid] => 52731
            [title] => Ziploc Kroger 'Giving Hope' Packaging
            [path] => node/52731
        )

    [3] => stdClass Object
        (
            [nid] => 53157
            [title] => Stacy's Kroger 'Giving Hope' Cut Cases
            [path] => node/53157
        )

    [4] => stdClass Object
        (
            [nid] => 52607
            [title] => Doritos Kroger 'Giving Hope' Packaging
            [path] => node/52607
        )

    [5] => stdClass Object
        (
            [nid] => 53164
            [title] => Nabisco Kroger 'Giving Hope' Violator
            [path] => node/53164
        )

    [6] => stdClass Object
        (
            [nid] => 64185
            [title] => Kroger 'Giving Hope' Stanchion Sign
            [path] => node/64185
        )

    [7] => stdClass Object
        (
            [nid] => 52190
            [title] => Betty Crocker Kroger 'Giving Hope' Shipper
            [path] => node/52190
        )

    [8] => stdClass Object
        (
            [nid] => 53159
            [title] => Frito-Lay Kroger 'Giving Hope' Packaging
            [path] => node/53159
        )

    [9] => stdClass Object
        (
            [nid] => 53162
            [title] => Kroger 'Giving Hope' Standee
            [path] => node/53162
        )

)

It's obviously doing something, but it's not sorting in ascending order according to nid. What do I need to do to get that to work?

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

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

发布评论

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

评论(1

可遇━不可求 2024-12-14 18:35:48

您的自定义排序函数需要返回值:

function my_search_sort($a, $b) {
  return strcmp($a->nid, $b->nid);
}

编辑
我已经更新了您引用的其他答案上的代码,它不正确。

Your custom sort function needs to return the value:

function my_search_sort($a, $b) {
  return strcmp($a->nid, $b->nid);
}

EDIT
I've updated the code on the other answer you referenced, it was incorrect.

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