Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 ?

share|improve this question
 
Do you just want to 'group by' the created date? I don't see any measurements in the object which suggest they would be useful in an aggregate function like sum() or avg(). Are items repeated? Guess I don't get what the 'Aggr date' is vs. the 'date' in your example desired output. –  ficuscr Aug 27 '12 at 18:01
add comment

2 Answers

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);
}
share|improve this answer
add comment

You can use usort to define your sorting function:

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...

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.