Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在foreach循环中生成PHP多维数组_Php_Arrays - Fatal编程技术网

在foreach循环中生成PHP多维数组

在foreach循环中生成PHP多维数组,php,arrays,Php,Arrays,我在当前月份的一个天数数组中循环,以生成另一个当天或之后的天数数组。然后我需要将其合并到我认为是多维数组中(以前没有使用过这些类型的数组)。下面是我的代码,它生成一个月的天数,并将每个天数与当前日期进行比较,以获得我的初始数组: // Set the default timezone date_default_timezone_set('Australia/Sydney'); $today = date("j"); $firstDayCurrentMonth = date("Y-m-01");

我在当前月份的一个天数数组中循环,以生成另一个当天或之后的天数数组。然后我需要将其合并到我认为是多维数组中(以前没有使用过这些类型的数组)。下面是我的代码,它生成一个月的天数,并将每个天数与当前日期进行比较,以获得我的初始数组:

// Set the default timezone
date_default_timezone_set('Australia/Sydney');
$today = date("j");
$firstDayCurrentMonth = date("Y-m-01");

// Get number of days in current month
$days = date("t");
// echo $days;

// Get array of all dates in current month
$aDates = array();
$oStart = new DateTime($firstDayCurrentMonth);
$oEnd = clone $oStart;
$oEnd->add(new DateInterval("P1M"));

while($oStart->getTimestamp() < $oEnd->getTimestamp()) {
    $aDates[] = $oStart->format('j');
    $oStart->add(new DateInterval("P1D"));
}                           

// Setup days array
$days = array();    

// Generate $linked_days array to feed into calendar files 
foreach($aDates as $date) {
    if($date >= $today) {
        $days[] = $date;
    }                    
}
例如,2013年12月2日的情况如下:

$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)));

我不知道如何将我的天数数组插入到大型多维数组中?

访问多维数组可以使用嵌套[],例如:

$allDays['2013']['12'] = array(...);
对于您的示例,我可能有一个更简单的代码:

<?php

$day = '2nd Dec, 2013';
$i = strtotime($day);

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

var_dump($allDays);

<?php

$day = '2nd Dec, 2013';
$i = strtotime($day);

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

var_dump($allDays);