I think I might have read every usort
article on *, 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
):
我想我可能已经阅读了*上的每篇文章,但我无法解决这个问题。可能是因为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
)
)
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:
我正在尝试最终对created_on值进行排序,但就目前而言,我决定能够对它们中的任何一个进行排序!当我尝试使用正常的cmp($ a,$ b)回调时,使用usort - 例如,在这个问题的答案中 - 我只是得到一个空白。例:
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.
print_r什么都没给我。这是PHP 5.2.n,而不是5.3 btw。
Guidance, please? And thankyou!
指导,好吗?谢谢你!
3 个解决方案
#1
4
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')
#2
3
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.
您对数组的转储表示元素是页面对象,而不是数组。一个偶然的机会,你需要说$ a-> created_on而不是$ a ['created_on']吗?使用对象表示法而不是数组表示法。
Just guessing...
#3
1
As @Tesserex suggests, you need to use object notation instead of array notation.
正如@Tesserex建议的那样,您需要使用对象表示法而不是数组表示法。
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.
另一件需要考虑的事情是,您的Pages并非都具有'created_on'属性,有些属性具有'published_on'属性。你必须在你的usort方法中做一些错误检查/逻辑,以确保你想要排序的密钥是可用的,并且当它没有时做一些事情。
#1
4
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')
#2
3
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.
您对数组的转储表示元素是页面对象,而不是数组。一个偶然的机会,你需要说$ a-> created_on而不是$ a ['created_on']吗?使用对象表示法而不是数组表示法。
Just guessing...
#3
1
As @Tesserex suggests, you need to use object notation instead of array notation.
正如@Tesserex建议的那样,您需要使用对象表示法而不是数组表示法。
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.
另一件需要考虑的事情是,您的Pages并非都具有'created_on'属性,有些属性具有'published_on'属性。你必须在你的usort方法中做一些错误检查/逻辑,以确保你想要排序的密钥是可用的,并且当它没有时做一些事情。