doufan1899 2013-12-02 14:00 采纳率: 100%
浏览 5
已采纳

PHP有条理地组合数组

I'm looping through an array of days in the current month to generate another array of days that are on or after the current day. I'm also doing the same for the next month (which will always include all days as they are after the current date).

The complexity is when the next month is in a different year to the current month. The format of the final array is like this:

array("year" => array("month" => array(days)));

When both months are in the same year it might look like this:

$allDays = array("2013" => array( "11" => array(28,29,30), "12" => array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31)));

When the 2 months are in different years (i.e. Dec and Jan) it might look like this:

$allDays = array("2013" => array("12" => array(2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31)), "2014" => array("1" => array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31) )) ;

Here's my code that generates the list of dates for the current month and the next month:

// Set the default timezone
    date_default_timezone_set('Australia/Sydney');

    // Get days for current month
    $day = date("Y-m-d");
    $i = strtotime($day);

    array("year" => array("month" => array(days)));
    $linked_days = array(
        date('Y', $i) => array(
            date('m') => range(date('d', $i), intval(date('t'))),
        ),
    );

    // Get days for next month

    $day2 = date("Y-m-d", strtotime('first day of next month')) ;
    $i2 = strtotime($day2);

    array("year" => array("month" => array(days)));
    $linked_days2 = array(
        date('Y', $i2) => array(
            date('m') => range(date('d', $i2), intval(date('t'))),
        ),
    );

I'm not sure how to go about combining them into the 1 array with a different sytanx if they are in the same year or not?

  • 写回答

1条回答 默认 最新

  • douxun4173 2013-12-02 14:05
    关注

    You can check if there is already an entry for the year in your array with isset function :

    Change this

    $day2 = date("Y-m-d", strtotime('first day of next month')) ;
    $i2 = strtotime($day2);
    
    array("year" => array("month" => array(days)));
    $linked_days2 = array(
        date('Y', $i2) => array(
            date('m') => range(date('d', $i2), intval(date('t'))),
        ),
    );
    

    To

    $day2 = date("Y-m-d", strtotime('first day of next month')) ;
    $i2 = strtotime($day2);
    
    array("year" => array("month" => array(days))); //useless line ??
    if(!isset($linked_days[date('Y', $i2)])){
        //if no entry for this year in array, create new entry
        $linked_days[date('Y', $i2)] = array(date('m') => range(date('d', $i), intval(date('t'))));
    }
    else{
        //else, just add the month entry
        $linked_days[date('Y', $i2)][date('m')] = range(date('d', $i2), intval(date('t'))) ;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)