2012-08-27 84 views
0

排序我有這樣的一個數組:PHP數組按日期stdClass的對象

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

等所表示陣列(它有很多的東西在裏面)英寸

現在顯示的是這樣

項目|日期
項目|日期
項目|日期

我想要做的就是採取陣列和骨料日期排序它

Somethink這樣

匯聚日期
項目|日期
項目|日期
累計日期
項目|日期
項目|日期

這是甚至有可能給這個數組?

+0

您是否希望按創建日期進行分組?我沒有看到對象中的任何測量結果,這些測量結果表明它們在像sum()或avg()這樣的集合函數中很有用。項目是否重複?猜測我沒有得到'Aggr date'與你的示例期望輸出中'date'的關係。 – ficuscr

回答

0

這是你在找什麼?

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

您可以使用usort來定義你的排序功能:

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

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

這將只是對它們進行排序。然後在循環輸出時,您可以根據日,小時,月份進行彙總...但是,您可以按照以下方式進行彙總: