php数组按日期排序stdClass对象


php array sorting by date stdClass Object

我有一个类似的数组:

Array ( [0] => stdClass Object ( [id] => 41 [title] => test2 [alias] => test2 [catid] => 8 [published] => 1 [introtext] =>
test2
[fulltext] => [video] => [gallery] => [extra_fields] => [] [extra_fields_search] => [created] => 2012-08-27 16:37:51 [created_by] => 62 [created_by_alias] => [checked_out] => 0 [checked_out_time] => 0000-00-00 00:00:00 [modified] => 0000-00-00 00:00:00 [modified_by] => 0 [publish_up] => 2012-08-27 16:37:51 [publish_down] => 0000-00-00 00:00:00 [trash] => 0 [access] => 1 [ordering] => 15 [featured] => 0 [featured_ordering] => 0 [image_caption] => [image_credits] => [video_caption] => [video_credits] => [hits] => 0 [params] => JParameter Object ( [_raw:protected] =>  

等等(它有很多东西)。

现在它是这样显示的

项目|日期
项目|日期
项目|日期

我想做的是获取该数组并按聚合日期进行排序

有点像这样

累计日期
项目|日期
项目|日期
累计日期
项目|日期
项目|日期

考虑到这个数组,这可能吗?

这就是您想要的吗?

$newArray = array();
foreach ($myArrayOfStdClasses as $key => $obj) {
    $newArray[date('Y-m-d', strtotime($obj->created]))[] = array('title' => $obj->tile, 'date' => $obj->created);
}

您可以使用usort定义您的排序函数:

usort($items, function($item1, $item2){
   if($item1->created == $item2->created)
      return 0;
   return $item1->created < $item2->created ? -1 : 1;
});

这只会对它们进行排序。然后在输出中循环它们时,您可以根据天、小时、月进行聚合。。。