How can I re-arrange a array of objects like this:
如何重新排列这样的对象数组:
[495] => stdClass Object
(
[date] => 2009-10-31 18:24:09
...
)
[582] => stdClass Object
(
[date] => 2010-2-11 12:01:42
...
)
...
by the date
key, oldest first ?
按日期键,最早的第一个?
4 个解决方案
#1
29
usort($array, function($a, $b) {
return strtotime($a['date']) - strtotime($b['date']);
});
Or if you don't have PHP 5.3:
或者如果你没有PHP 5.3:
function cb($a, $b) {
return strtotime($a['date']) - strtotime($b['date']);
}
usort($array, 'cb');
#2
12
Since the original question is about sorting arrays of stdClass() objects, here's the code which would work if $a and $b are objects:
由于最初的问题是关于对stdClass()对象的数组进行排序,所以如果$ a和$ b是对象,这里的代码将起作用:
usort($array, function($a, $b) {
return strtotime($a->date) - strtotime($b->date);
});
Or if you don't have PHP 5.3:
或者如果你没有PHP 5.3:
function cb($a, $b) {
return strtotime($a->date) - strtotime($b->date);
}
usort($array, 'cb');
#3
3
I wanted to expand on arnaud576875
's answer. I ran across this same issue, but with using DateTime objects. This is how I was able to accomplish the same thing.
我想扩展arnaud576875的答案。我遇到了同样的问题,但是使用了DateTime对象。这就是我能够完成同样的事情。
usort($array, function($a, $b) {
return $a['date']->format('U') - $b['date']->format('U');
});
#4
0
I wanted to expand on arnaud576875 and Michael Irigoyen.
我想扩展arnaud576875和Michael Irigoyen。
Same issue with object containing dateTime with Symphony.
与包含Symphony的dateTime的对象相同的问题。
I coudn't use $a['date'] because it was not an key array.
我不能使用$ a ['date'],因为它不是一个关键数组。
usort($verifications, function($a, $b) {
return $a->getDate()->format('U') - $b->getDate()->format('U');
});
This solved my problem
这解决了我的问题
#1
29
usort($array, function($a, $b) {
return strtotime($a['date']) - strtotime($b['date']);
});
Or if you don't have PHP 5.3:
或者如果你没有PHP 5.3:
function cb($a, $b) {
return strtotime($a['date']) - strtotime($b['date']);
}
usort($array, 'cb');
#2
12
Since the original question is about sorting arrays of stdClass() objects, here's the code which would work if $a and $b are objects:
由于最初的问题是关于对stdClass()对象的数组进行排序,所以如果$ a和$ b是对象,这里的代码将起作用:
usort($array, function($a, $b) {
return strtotime($a->date) - strtotime($b->date);
});
Or if you don't have PHP 5.3:
或者如果你没有PHP 5.3:
function cb($a, $b) {
return strtotime($a->date) - strtotime($b->date);
}
usort($array, 'cb');
#3
3
I wanted to expand on arnaud576875
's answer. I ran across this same issue, but with using DateTime objects. This is how I was able to accomplish the same thing.
我想扩展arnaud576875的答案。我遇到了同样的问题,但是使用了DateTime对象。这就是我能够完成同样的事情。
usort($array, function($a, $b) {
return $a['date']->format('U') - $b['date']->format('U');
});
#4
0
I wanted to expand on arnaud576875 and Michael Irigoyen.
我想扩展arnaud576875和Michael Irigoyen。
Same issue with object containing dateTime with Symphony.
与包含Symphony的dateTime的对象相同的问题。
I coudn't use $a['date'] because it was not an key array.
我不能使用$ a ['date'],因为它不是一个关键数组。
usort($verifications, function($a, $b) {
return $a->getDate()->format('U') - $b->getDate()->format('U');
});
This solved my problem
这解决了我的问题