php - grouping array data using a field value -


i have array newsfeed containing last followers of current user, follows:

$newsfeed=array( 1 => array( 'follower'=>'1', 'following'=>'5', 'datecreation'=>'2013-07-12 15:10:34' ), 2=>array( 'follower'=>'6', 'following'=>'5', 'datecreation'=>'2013-07-12 12:30:56' ), 3=>array( 'follower'=>'7', 'following'=>'5', 'datecreation'=>'2013-05-12 19:08:00' ) ); 

what want do, shorten news feed, puting items of same day in 1 array , item 1 , 2 in previous table. follows:

 $newsfeed=array(     1 => array(     'follower'=>array('1','6'),     'following'=>'5',     'datecreation'=>'2013-07-12'     ),     3=>array(     'follower'=>'7',     'following'=>'5',     'datecreation'=>'2013-05-12 19:08:00'     )     ); 

notice in result, dropped array key=2 , key=1, , put followers' ids in array, put in new array.

is there php function can ?

finally, found solution, suggested 1 ... more edits may come in future regarding possible errors or exceptions.

this suggested solution

$creationdate1 = false;         foreach ($newsfeed $key => $feed) {              /*             * **followers** */             if ($feed['type'] == 'follow') { //test if current row follow type                 $creationdate = datetime::createfromformat("y-m-d h:i:s", $feed['creationdate']);                 $creationdate = $creationdate->format('y-m-d'); //drop h:i:s date                  if ($creationdate != $creationdate1) {                       $followkey = $key;                     $followersarray = array();                 }                   $followersarray[] = $feed['follower']; //array followers ids                   unset($newsfeed[$key]);//remove current row                  if ($creationdate != $creationdate1) { // if follow has not same date previous 1                       $newsfeed[$followkey] = array(                         'follower' => $followersarray,                         'following' => $feed['following'],                         'creationdate' => $feed['creationdate']                     );//add row, datetime                 } else { //add row, date, , without time.                     $newsfeed[$followkey] = array(                         'follower' => $followersarray,                         'following' => $feed['following'],                         'creationdate' => $creationdate                     );                 }                  $creationdate1 = $creationdate; //$creationdate1=date of current row.             }         }  

any improvements, edits or suggestions appreciated


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -