我应该如何使用 usort 按键对这个数组进行排序?

发布于 2024-11-05 17:21:53 字数 1359 浏览 1 评论 0 原文

我想我可能已经阅读了 StackOverflow 上的每一篇 usort 文章,但我无法理解这篇文章。可能 usort 不是我需要的工具?这是我正在使用的数组的一部分(我将其分配给 $allPages):

Array
(
    [0] => Page Object
        (
            [id] => 4
            [slug] => articles
            [created_on] => 2009-08-06 07:16:00
        )

    [1] => Page Object
        (
            [id] => 99
            [slug] => a-brief-history
            [created_on] => 2011-04-25 12:07:26
        )

    [2] => Page Object
        (
            [id] => 98
            [slug] => we-arrive
            [created_on] => 2011-04-24 13:52:35
        )

    [3] => Page Object
        (
            [id] => 83
            [slug] => new-year
            [created_on] => 2011-01-02 14:05:12
        )
)

我最终尝试对 created_on 值进行排序,但是对于那一刻,我决定能够对其中任何一个进行排序!当我尝试使用 usort 进行正常的 cmp($a, $b) 类型回调时——例如,在 这个关于 usort 问题的答案 - 我刚刚得到一个 空白的。示例:

function cmp($a, $b) {
  return strcmp($a["slug"], $b["slug"]);
}
usort($allPages, 'cmp')

print_r 没有给我任何结果。这是 PHP 5.2.n 的情况,而不是 5.3 顺便说一句。

请指导一下?谢谢你!

I think I might have read every usort article on StackOverflow, but I can't work out this one. It might be that usort is not the tool I need? Here's a bit of the array that I'm working with (I have it assigned to $allPages):

Array
(
    [0] => Page Object
        (
            [id] => 4
            [slug] => articles
            [created_on] => 2009-08-06 07:16:00
        )

    [1] => Page Object
        (
            [id] => 99
            [slug] => a-brief-history
            [created_on] => 2011-04-25 12:07:26
        )

    [2] => Page Object
        (
            [id] => 98
            [slug] => we-arrive
            [created_on] => 2011-04-24 13:52:35
        )

    [3] => Page Object
        (
            [id] => 83
            [slug] => new-year
            [created_on] => 2011-01-02 14:05:12
        )
)

I am trying ultimately to sort on the created_on value, but for the moment, I'd settle on being able to sort on any of them! When I try the normal cmp($a, $b) type callback with usort -- as, for example, in this answer on a usort question -- I just get a blank. Example:

function cmp($a, $b) {
  return strcmp($a["slug"], $b["slug"]);
}
usort($allPages, 'cmp')

And print_r gives me nothing. This is with PHP 5.2.n, not 5.3 btw.

Guidance, please? And thankyou!

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

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

发布评论

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

评论(3

就此别过 2024-11-12 17:21:53

数组中的项目是对象,而不是关联数组,因此您需要像这样引用它们:

function cmp($a, $b) {
  return strcmp($a->slug, $b->slug);
}
usort($allPages, 'cmp')

Your items in the array are objects, not associative arrays, so you need to refer to them like this:

function cmp($a, $b) {
  return strcmp($a->slug, $b->slug);
}
usort($allPages, 'cmp')
用心笑 2024-11-12 17:21:53

您的数组转储表明元素是页面对象,而不是数组。碰巧,您需要说 $a->created_on 而不是 $a['created_on'] 吗?使用对象表示法而不是数组表示法。

只是猜测...

Your dump of the array says that the elements are Page Objects, not arrays. By chance, do you need to say $a->created_on instead of $a['created_on']? Using object notation instead of array notation.

Just guessing...

白鸥掠海 2024-11-12 17:21:53

正如@Tesserex 所建议的,您需要使用对象表示法而不是数组表示法。

如果您打开了通知,您会收到有关以数组形式访问对象的错误消息。

另一件需要考虑的事情是,您的页面并不都具有“created_on”属性,有些页面具有“published_on”属性。您必须在 usort 方法中进行一些错误检查/逻辑,以确保您想要排序的键可用,并在不可用时执行某些操作。

As @Tesserex suggests, you need to use object notation instead of array notation.

If you had notices turned on, you'd get error messages about accessing an object as an array.

Another thing to consider, is, your Pages don't all have a 'created_on' attribute, some have a 'published_on' attribute. You'll have to do some error checking / logic inside your usort method to make sure that the key you want to sort by is available, and do something when it's not.

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