phparraysmultidimensional-arraygroupingsub-array

Group rows of a 2d array by the year and month values from a year-month column to create subarrays in each group


I have this main $rr array.

Array
  (
   [0] => Array
    (
        [id] => 162
        [content] => Kingpabel ATN     - Copy (5) - Copy 20150623113618
        [page_name] => all_users
        [type] => all_users
        [image] => Kingpabel ATN     - Copy (5) - Copy 20150623113618.pdf
        [date_upload] => 2015-01
    )

[1] => Array
    (
        [id] => 144
        [content] => Kingpabel ATN     - Copy (5) - Copy 20150623111159
        [page_name] => all_users
        [type] => all_users
        [image] => Kingpabel ATN     - Copy (5) - Copy 20150623111159.pdf
        [date_upload] => 2015-05
    )

[2] => Array
    (
        [id] => 145
        [content] => Kingpabel ATN     - Copy (6) - Copy 20150623111159
        [page_name] => all_users
        [type] => all_users
        [image] => Kingpabel ATN     - Copy (6) - Copy 20150623111159.pdf
        [date_upload] => 2015-05
    )



[3] => Array
    (
        [id] => 129
        [content] => Kingpabel ATN     - Copy (6) - Copy 20150623094147
        [page_name] => all_users
        [type] => all_users
        [image] => Kingpabel ATN     - Copy (6) - Copy 20150623094147.pdf
        [date_upload] => 2015-06
    )

[4] => Array
    (
        [id] => 165
        [content] => Kingpabel .ATN.     - Copy. - Copy (2) 20150624110802
        [page_name] => all_users
        [type] => all_users
        [image] => Kingpabel .ATN.     - Copy. - Copy (2) 20150624110802.pdf
        [date_upload] => 2016-01
    )

  )
  

i am tring this code for making array of year and month:

 while($rr=mysql_fetch_array($r))
    {
        //$dall[]=$rr;
        $dt=explode("-",$rr['date_upload']);
         if(count($year)>0)
        {
            if(array_key_exists($dt[0],$year))
            {  
                if(!in_array($dt[1],$year[$dt[0]]))
                {
                 array_push( $year[$dt[0]], $dt[1]);
                    
                }
                  }
            else
            {
                     $ye=$dt[0];
               $year[$ye]= array($dt[1]);
                
           }
        }
        else
        {
            $ye=$dt[0];
            $year[$ye]=array($dt[1]);
         }
          
        
    }
    

i got this result from above code:

       Array
      (
       [2015] => Array
           (
              [0] => 01
              [1] => 05
              [2] => 06
    )

        [2016] => Array
        (
           [0] => 01
        )

)

i am trying to make array like this according to date_upload field in $rr array above. what modificationas are needed in above code for making array as below?

Array
(
[2015] => Array
    (
        [01] => array(
                     [0]=>array(
                              
                                [id] => 162
                                [content] => Kingpabel ATN     - Copy (5) - Copy 20150623113618
                                [page_name] => all_users
                                [type] => all_users
                                [image] => Kingpabel ATN     - Copy (5) - Copy 20150623113618.pdf
                                [date_upload] => 2015-01
                        )
            )
        [05] => array( 
                    [0]=>array(
                              
                                [id] => 144
                                [content] => Kingpabel ATN     - Copy (5) - Copy 20150623113618
                                [page_name] => all_users
                                [type] => all_users
                                [image] => Kingpabel ATN     - Copy (5) - Copy 20150623113618.pdf
                                [date_upload] => 2015-05
                            )
                    [1]=>array(
                                [id] => 145
                                [content] => Kingpabel ATN     - Copy (5) - Copy 20150623113618
                                [page_name] => all_users
                                [type] => all_users
                                [image] => Kingpabel ATN     - Copy (5) - Copy 20150623113618.pdf
                                [date_upload] => 2015-05
                             )

                    )
        [06] => array(
                        [0]=>array(
                              
                                [id] => 129
                                [content] => Kingpabel ATN     - Copy (5) - Copy 20150623113618
                                [page_name] => all_users
                                [type] => all_users
                                [image] => Kingpabel ATN     - Copy (5) - Copy 20150623113618.pdf
                                [date_upload] => 2015-06
                            )
        
        
        )
    )

[2016] => Array
    (
        [01] => array(
                        [0]=>array(
                              
                                [id] => 165
                                [content] => Kingpabel ATN     - Copy (5) - Copy 20150623113618
                                [page_name] => all_users
                                [type] => all_users
                                [image] => Kingpabel ATN     - Copy (5) - Copy 20150623113618.pdf
                                [date_upload] => 2015-05
                            )
        
        )
    )

)
 

Solution

  • You can try:

     $resultant = array();
      foreach($rr as $single) {
       $resultant[date('Y',strtotime($single['date_upload']))][date('m',strtotime($single['date_upload']))][]= $single;
      }
    
     print_r($resultant);