首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何按月和年排列数组中的数据

如何按月和年排列数组中的数据
EN

Stack Overflow用户
提问于 2013-09-11 12:06:15
回答 1查看 504关注 0票数 0

我正在开发一个在php zend框架和sql server2008的网站。

我需要创建一个多维数组,以这种格式按年和月存储数据:

代码语言:javascript
复制
Array(
  [2012] => Array(
      [11] => Array(           
         // data
    )
      [12] => Array(
        // data
      )

  )

  [2013] => Array(
      [01] => Array(           
         // data
    )
      [02] => Array(
        // data
      )
      [03] => Array(           
         // data
    )
      [04] => Array(
        // data
      )
  )
)

但是我在我的结果数组中得到了重复的结果。这意味着我也会在2013年获得2012年的数据。我的代码如下:

代码语言:javascript
复制
 // Database functions to get data

 function select_cldata($month,$year)
    {
      global $db;
       $select = "select ClientId,ClientName,Email,Request,'Order Placed' as info from tbl_clrequest where MONTH(CreatedDate) = ".$month." and YEAR(CreatedDate) = ".$year;
       return $db->fetchAll($select);
    }

function select_itemsdata($month,$year)
{
   global $db;
   $select = "select ItemId,ItemName,Desc,price,'Items delivered' as info1 from tbl_items where MONTH(ItemDate) = ".$month." and YEAR(ItemDate) = ".$year;
   return $db->fetchAll($select);
}

// PHP Code
$test = new dbase();
$months = "2012-11,2012-12,2013-01,2013-02,2013-03,2013-04";
$mon_num = explode(",",$months);
$res = array();
$result = array();
foreach($mon_num as $mn)
{   
    $mm = explode("-",$mn);
    $year = $mm[0];
    $month = $mm[1];
    $cl_data = $test->select_cldata($month,$year);
    $dl_data = $test->select_itemsdata($month,$year);
    $res[$month] = array_merge($clients,$sr,$events,$ses,$docs,$event_change,$devents);
    $result[$year] = $res;  
}
echo "<pre>";                               
print_r($result);die;

有谁能帮帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2013-09-11 17:08:06

下面是您使用我的工作示例的解决方案

代码语言:javascript
复制
foreach($mon_num as $mn)
{   
    $mm = explode("-",$mn);    
    $year = $mm[0];
    $month = $mm[1];
    $cl_data = $test->select_cldata($month,$year);
    $dl_data = $test->select_itemsdata($month,$year);
    $res = array_merge($clients,$sr,$events,$ses,$docs,$event_change,$devents);
    $result[$year][$month] = $res;  
}

我添加了一些要打印的虚拟数据

输出

代码语言:javascript
复制
Array
(
    [2012] => Array
        (
            [11] => Array
                (
                    [0] => 12
                    [1] => 1211
                    [2] => 112122
                    [3] => 121111
                    [4] => 67676
                    [5] => 545454
                    [6] => 666666
                )

            [12] => Array
                (
                    [0] => 12
                    [1] => 1211
                    [2] => 112122
                    [3] => 121111
                    [4] => 67676
                    [5] => 545454
                    [6] => 666666
                )

        )

    [2013] => Array
        (
            [01] => Array
                (
                    [0] => 12
                    [1] => 1211
                    [2] => 112122
                    [3] => 121111
                    [4] => 67676
                    [5] => 545454
                    [6] => 666666
                )

            [02] => Array
                (
                    [0] => 12
                    [1] => 1211
                    [2] => 112122
                    [3] => 121111
                    [4] => 67676
                    [5] => 545454
                    [6] => 666666
                )

            [03] => Array
                (
                    [0] => 12
                    [1] => 1211
                    [2] => 112122
                    [3] => 121111
                    [4] => 67676
                    [5] => 545454
                    [6] => 666666
                )

            [04] => Array
                (
                    [0] => 12
                    [1] => 1211
                    [2] => 112122
                    [3] => 121111
                    [4] => 67676
                    [5] => 545454
                    [6] => 666666
                )

        )

)

希望这一定能解决你的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18732492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档