从数组创建新数组

时间:2015-06-25 05:27:36

标签: php arrays

我有这个主$ rr数组。

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
    )

  )

我将这段代码用于制作年份和月份:

 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]);
         }


    }

我从上面的代码得到了这个结果:

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

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

)

我正在尝试按照上面$ rr数组中的date_upload字段制作这样的数组。 以上代码中需要进行哪些修改才能生成数组?

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
                            )

        )
    )

)

2 个答案:

答案 0 :(得分:3)

您可以尝试:

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

 print_r($resultant);

答案 1 :(得分:2)

使用字符串函数提取数据:

SELECT *
 FROM dt
ORDER BY SUBSTR(date_upload,1,4), SUBSTR(date_upload,6,2)