简体   繁体   English

php数组按日期stdClass对象排序

[英]php array sorting by date stdClass Object

I have an array of something like this: 我有这样的数组:

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] =>  

and etc in that array ( it has alot of things in it ). 等等(在其中有很多东西)。

Now it is displaying like this 现在它像这样显示

Item | 项目| date 日期
Item | 项目| date 日期
Item | 项目| date 日期

What I want to do is take that array and sort it by aggregate date 我想做的是获取该数组并按汇总日期对其进行排序

Somethink like this 像这样

Aggr date 总日期
Item | 项目| date 日期
Item | 项目| date 日期
Aggr date 总日期
Item | 项目| date 日期
Item | 项目| date 日期

Is this even possible given this array ? 给定这个数组有可能吗?

Is this what you are looking for? 这是你想要的?

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

You can use usort to define your sorting function: 您可以使用usort定义排序功能:

usort($items, function($item1, $item2){
   if($item1->created == $item2->created)
      return 0;

   return $item1->created < $item2->created ? -1 : 1;
});

This will just sort them. 这将对它们进行排序。 Then on output as you loop through them you can do the aggregation based on day, hour, month however... 然后在您遍历它们的输出上,您可以根据日,小时,月进行汇总,但是...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM