如何使用PHP中的日期重新排列/重组数组?

时间:2017-12-15 22:03:51

标签: php arrays loops date

我有一个日期在10:30到11点之间的数组。这些项目按时间顺序列出。

我正在寻找一个PHP解决方案来重新排列数组的结构或智能循环,这样我就可以按照自己的意愿组织日期。

我需要回应的是: (旧尝试,请忽略此示例。只需将其保留在已提供的解决方案中。请参阅下面的更新。)

'1', '1, 10, 2017', '1, 11, 2017'
'2', '2, 10, 2017', '2, 11, 2017',
...
'30', '30, 10, 2017', '30, 11, 2017',
'31', '31, 10, 2017', ''

请注意,11月(11)只有30天,所以没有第31个条目。

所以我需要一个遍历数组的循环,并以相同的开始日回显所有日期,然后执行<br>或类似的操作。它总是应该在第31位结束,有没有数据。

更新
我的输出需要略有不同:

['1', '1, 10, 2017', '1, 11, 2017'],
['2', '2, 10, 2017', '2, 11, 2017'],
...
['30', '30, 10, 2017', '30, 11, 2017'],
['31', '31, 10, 2017', '0, 0, 0']

主要变化是:

  • 方括号
  • 如果没有值(例如11月31日不存在),我需要像以前一样输出'0, 0, 0'而不是空字符串。

背景是我有一个必然需要任何值的函数,可能是0。

我尝试了不同的东西(将日期,月份和年份分开来分离数组/使用for-loops /使用foreach-loops)但没有真正起作用。我的主要问题似乎是每个月都有不同的天数。

您是否有任何建议如何使其正常工作? 尝试重组阵列可能会更好吗?还是有其他解决方案?

代码应该适用于不同的日期范围(不仅仅是我上面提到的那些)。

这是我的阵列:

    Array
(
    [0] => 1, 10, 2017
    [1] => 2, 10, 2017
    [2] => 3, 10, 2017
    [3] => 4, 10, 2017
    [4] => 5, 10, 2017
    [5] => 6, 10, 2017
    [6] => 7, 10, 2017
    [7] => 8, 10, 2017
    [8] => 9, 10, 2017
    [9] => 10, 10, 2017
    [10] => 11, 10, 2017
    [11] => 12, 10, 2017
    [12] => 13, 10, 2017
    [13] => 14, 10, 2017
    [14] => 15, 10, 2017
    [15] => 16, 10, 2017
    [16] => 17, 10, 2017
    [17] => 18, 10, 2017
    [18] => 19, 10, 2017
    [19] => 20, 10, 2017
    [20] => 21, 10, 2017
    [21] => 22, 10, 2017
    [22] => 23, 10, 2017
    [23] => 24, 10, 2017
    [24] => 25, 10, 2017
    [25] => 26, 10, 2017
    [26] => 27, 10, 2017
    [27] => 28, 10, 2017
    [28] => 29, 10, 2017
    [29] => 30, 10, 2017
    [30] => 31, 10, 2017
    [31] => 1, 11, 2017
    [32] => 2, 11, 2017
    [33] => 3, 11, 2017
    [34] => 4, 11, 2017
    [35] => 5, 11, 2017
    [36] => 6, 11, 2017
    [37] => 7, 11, 2017
    [38] => 8, 11, 2017
    [39] => 9, 11, 2017
    [40] => 10, 11, 2017
    [41] => 11, 11, 2017
    [42] => 12, 11, 2017
    [43] => 13, 11, 2017
    [44] => 14, 11, 2017
    [45] => 15, 11, 2017
    [46] => 16, 11, 2017
    [47] => 17, 11, 2017
    [48] => 18, 11, 2017
    [49] => 19, 11, 2017
    [50] => 20, 11, 2017
    [51] => 21, 11, 2017
    [52] => 22, 11, 2017
    [53] => 23, 11, 2017
    [54] => 24, 11, 2017
    [55] => 25, 11, 2017
    [56] => 26, 11, 2017
    [57] => 27, 11, 2017
    [58] => 28, 11, 2017
    [59] => 29, 11, 2017
    [60] => 30, 11, 2017
)

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

我建议甚至不做那样的事。日期和时间不是你想要手动处理的东西,因为它们比它们看起来要复杂得多。

<?php
$period = new DatePeriod(
    new DateTime('2017-01-01'), // start
    new DateInterval('P1M'),    // interval of 1 month
    new DateTime('2018-01-01')  // end
);

foreach( $period as $date ) {
    printf("%s\n", $date->format('Y-m-d'));
}

输出:

2017-01-01
2017-02-01
2017-03-01
2017-04-01
2017-05-01
2017-06-01
2017-07-01
2017-08-01
2017-09-01
2017-10-01
2017-11-01
2017-12-01

http://php.net/manual/en/class.datetime.php
http://php.net/manual/en/class.dateperiod.php
http://php.net/manual/en/class.dateinterval.php
http://php.net/manual/en/book.datetime.php

正如经验法则所示:如果您发现自己手动定义了大量重复的数组,那么您应该退后一步并重新检查您的方法。

答案 1 :(得分:0)

我全心全意地同意@Sammitch对此的看法。如果必须使用此数组,则可以遍历数组并使用strtotime在Unix时间内转换条目:

foreach($date_array as $key => $value) {
  $value = explode(",",$value);
  $value = trim($value[2])."-".trim($value[1])."-".trim($value[0]);
  $value = strtotime($value);
  $date_array[$key] = $value;
}

如果您想要按原样处理数组,可以使您接近所需内容:

<?php
$date_array = array(
    0 => "1, 10, 2017",
    1 => "2, 10, 2017",
    2 => "3, 10, 2017",
    3 => "4, 10, 2017",
    4 => "5, 10, 2017",
    5 => "6, 10, 2017",
    6 => "7, 10, 2017",
    7 => "8, 10, 2017",
    8 => "9, 10, 2017",
    9 => "10, 10, 2017",
    10 => "11, 10, 2017",
    11 => "12, 10, 2017",
    12 => "13, 10, 2017",
    13 => "14, 10, 2017",
    14 => "15, 10, 2017",
    15 => "16, 10, 2017",
    16 => "17, 10, 2017",
    17 => "18, 10, 2017",
    18 => "19, 10, 2017",
    19 => "20, 10, 2017",
    20 => "21, 10, 2017",
    21 => "22, 10, 2017",
    22 => "23, 10, 2017",
    23 => "24, 10, 2017",
    24 => "25, 10, 2017",
    25 => "26, 10, 2017",
    26 => "27, 10, 2017",
    27 => "28, 10, 2017",
    28 => "29, 10, 2017",
    29 => "30, 10, 2017",
    30 => "31, 10, 2017",
    31 => "1, 11, 2017",
    32 => "2, 11, 2017",
    33 => "3, 11, 2017",
    34 => "4, 11, 2017",
    35 => "5, 11, 2017",
    36 => "6, 11, 2017",
    37 => "7, 11, 2017",
    38 => "8, 11, 2017",
    39 => "9, 11, 2017",
    40 => "10, 11, 2017",
    41 => "11, 11, 2017",
    42 => "12, 11, 2017",
    43 => "13, 11, 2017",
    44 => "14, 11, 2017",
    45 => "15, 11, 2017",
    46 => "16, 11, 2017",
    47 => "17, 11, 2017",
    48 => "18, 11, 2017",
    49 => "19, 11, 2017",
    50 => "20, 11, 2017",
    51 => "21, 11, 2017",
    52 => "22, 11, 2017",
    53 => "23, 11, 2017",
    54 => "24, 11, 2017",
    55 => "25, 11, 2017",
    56 => "26, 11, 2017",
    57 => "27, 11, 2017",
    58 => "28, 11, 2017",
    59 => "29, 11, 2017",
    60 => "30, 11, 2017"
);
for($i=1;$i<=31;$i++) {
  $line = "'".$i."', ";
  foreach($date_array as $key => $value) {
    $exp = explode(",",$value);
    if($exp[0] == $i) {
        $line .= "'".$value."', ";
    }
  }
  echo $line."<br />";
}

?>

输出:

'1','1,10,2017','1,11,2017',

'2','2017年2月10日','2017年2月11日',

...

'30','30,10,2017','30,11,2017',

'31','31,10,2017',

答案 2 :(得分:0)

看来您所需的结果是一种伪数组字符串。如果您打算将其用作数组或json,我可以优化此代码以更好地满足您的最终要求。

代码展示3个不同的起点:(Demo

function buildPseudoArray($month1,$year1){ // default dates in params: $month1=date('m'),$year1=date('Y') causes Fatal error
    echo "Result for $month1-$year1:\n";
    $m1_last=date('t',strtotime("$year1-$month1"));
    if($month1==12){
        $year2=$year1+1;
        $month2=1;
    }else{
        $year2=$year1;
        $month2=$month1+1;
    }
    $m2_last=date('t',strtotime("$year2-$month2"));
    $result='';
    for($i=1,$stop=31; $i<=$stop; ++$i){
        $result.="['$i', ";
        $result.=$i<=$m1_last ? "'$i, $month1, $year1', " : "'0, 0, 0', ";  // conditionally fill missing dates
        $result.=$i<=$m2_last ? "'$i, $month2, $year2'" : "'0, 0, 0'";  // conditionally fill missing dates
        $result.=']'.($i<$stop ? ',' : '')."\n";  // conditionally add comma to end of row
    }
    return $result;
}

echo buildPseudoArray(1,2017);  // shows 0, 0, 0 in first column
echo "\n---------------------\n";
echo buildPseudoArray('2',2017);  // shows 0, 0, 0 in second column
echo "\n---------------------\n";
echo buildPseudoArray(12,2017);  // shows incrementation into next year

输出:

Result for 1-2017:
['1', '1, 1, 2017', '1, 2, 2017'],
['2', '2, 1, 2017', '2, 2, 2017'],
['3', '3, 1, 2017', '3, 2, 2017'],
['4', '4, 1, 2017', '4, 2, 2017'],
['5', '5, 1, 2017', '5, 2, 2017'],
['6', '6, 1, 2017', '6, 2, 2017'],
['7', '7, 1, 2017', '7, 2, 2017'],
['8', '8, 1, 2017', '8, 2, 2017'],
['9', '9, 1, 2017', '9, 2, 2017'],
['10', '10, 1, 2017', '10, 2, 2017'],
['11', '11, 1, 2017', '11, 2, 2017'],
['12', '12, 1, 2017', '12, 2, 2017'],
['13', '13, 1, 2017', '13, 2, 2017'],
['14', '14, 1, 2017', '14, 2, 2017'],
['15', '15, 1, 2017', '15, 2, 2017'],
['16', '16, 1, 2017', '16, 2, 2017'],
['17', '17, 1, 2017', '17, 2, 2017'],
['18', '18, 1, 2017', '18, 2, 2017'],
['19', '19, 1, 2017', '19, 2, 2017'],
['20', '20, 1, 2017', '20, 2, 2017'],
['21', '21, 1, 2017', '21, 2, 2017'],
['22', '22, 1, 2017', '22, 2, 2017'],
['23', '23, 1, 2017', '23, 2, 2017'],
['24', '24, 1, 2017', '24, 2, 2017'],
['25', '25, 1, 2017', '25, 2, 2017'],
['26', '26, 1, 2017', '26, 2, 2017'],
['27', '27, 1, 2017', '27, 2, 2017'],
['28', '28, 1, 2017', '28, 2, 2017'],
['29', '29, 1, 2017', '0, 0, 0'],
['30', '30, 1, 2017', '0, 0, 0'],
['31', '31, 1, 2017', '0, 0, 0']

---------------------
Result for 2-2017:
['1', '1, 2, 2017', '1, 3, 2017'],
['2', '2, 2, 2017', '2, 3, 2017'],
['3', '3, 2, 2017', '3, 3, 2017'],
['4', '4, 2, 2017', '4, 3, 2017'],
['5', '5, 2, 2017', '5, 3, 2017'],
['6', '6, 2, 2017', '6, 3, 2017'],
['7', '7, 2, 2017', '7, 3, 2017'],
['8', '8, 2, 2017', '8, 3, 2017'],
['9', '9, 2, 2017', '9, 3, 2017'],
['10', '10, 2, 2017', '10, 3, 2017'],
['11', '11, 2, 2017', '11, 3, 2017'],
['12', '12, 2, 2017', '12, 3, 2017'],
['13', '13, 2, 2017', '13, 3, 2017'],
['14', '14, 2, 2017', '14, 3, 2017'],
['15', '15, 2, 2017', '15, 3, 2017'],
['16', '16, 2, 2017', '16, 3, 2017'],
['17', '17, 2, 2017', '17, 3, 2017'],
['18', '18, 2, 2017', '18, 3, 2017'],
['19', '19, 2, 2017', '19, 3, 2017'],
['20', '20, 2, 2017', '20, 3, 2017'],
['21', '21, 2, 2017', '21, 3, 2017'],
['22', '22, 2, 2017', '22, 3, 2017'],
['23', '23, 2, 2017', '23, 3, 2017'],
['24', '24, 2, 2017', '24, 3, 2017'],
['25', '25, 2, 2017', '25, 3, 2017'],
['26', '26, 2, 2017', '26, 3, 2017'],
['27', '27, 2, 2017', '27, 3, 2017'],
['28', '28, 2, 2017', '28, 3, 2017'],
['29', '0, 0, 0', '29, 3, 2017'],
['30', '0, 0, 0', '30, 3, 2017'],
['31', '0, 0, 0', '31, 3, 2017']

---------------------
Result for 12-2017:
['1', '1, 12, 2017', '1, 1, 2018'],
['2', '2, 12, 2017', '2, 1, 2018'],
['3', '3, 12, 2017', '3, 1, 2018'],
['4', '4, 12, 2017', '4, 1, 2018'],
['5', '5, 12, 2017', '5, 1, 2018'],
['6', '6, 12, 2017', '6, 1, 2018'],
['7', '7, 12, 2017', '7, 1, 2018'],
['8', '8, 12, 2017', '8, 1, 2018'],
['9', '9, 12, 2017', '9, 1, 2018'],
['10', '10, 12, 2017', '10, 1, 2018'],
['11', '11, 12, 2017', '11, 1, 2018'],
['12', '12, 12, 2017', '12, 1, 2018'],
['13', '13, 12, 2017', '13, 1, 2018'],
['14', '14, 12, 2017', '14, 1, 2018'],
['15', '15, 12, 2017', '15, 1, 2018'],
['16', '16, 12, 2017', '16, 1, 2018'],
['17', '17, 12, 2017', '17, 1, 2018'],
['18', '18, 12, 2017', '18, 1, 2018'],
['19', '19, 12, 2017', '19, 1, 2018'],
['20', '20, 12, 2017', '20, 1, 2018'],
['21', '21, 12, 2017', '21, 1, 2018'],
['22', '22, 12, 2017', '22, 1, 2018'],
['23', '23, 12, 2017', '23, 1, 2018'],
['24', '24, 12, 2017', '24, 1, 2018'],
['25', '25, 12, 2017', '25, 1, 2018'],
['26', '26, 12, 2017', '26, 1, 2018'],
['27', '27, 12, 2017', '27, 1, 2018'],
['28', '28, 12, 2017', '28, 1, 2018'],
['29', '29, 12, 2017', '29, 1, 2018'],
['30', '30, 12, 2017', '30, 1, 2018'],
['31', '31, 12, 2017', '31, 1, 2018']

这是Google图表任务的准备好的代码:Demo Link

function getOrdersByDay($i,$m,$y){
    return rand(0,50);  // put your stuff in here
}

function buildPseudoArray($month1,$year1){ // default dates in params: $month1=date('m'),$year1=date('Y') causes Fatal error
    echo "Result for $month1-$year1:\n";
    $m1_last=date('t',strtotime("$year1-$month1"));
    if($month1==12){
        $year2=$year1+1;
        $month2=1;
    }else{
        $year2=$year1;
        $month2=$month1+1;
    }
    $m2_last=date('t',strtotime("$year2-$month2"));
    $result='[';
    $result.="['Day', '".date('M Y',strtotime("$year1-$month1"))."', '".date('M Y',strtotime("$year2-$month2"))."'],\n";
    for($i=1,$stop=31; $i<=$stop; ++$i){
        $result.="['".str_pad($i,2,0,STR_PAD_LEFT).".', ";  // pad single digit days with a leading zero, add trailing dot
        $result.="'".($i<=$m1_last ? getOrdersByDay($i,$month1,$year1) : getOrdersByDay(0,0,0))."', ";  // conditionally parameters
        $result.="'".($i<=$m2_last ? getOrdersByDay($i,$month2,$year2) : getOrdersByDay(0,0,0))."', ";  // conditionally parameters
        $result.=']'.($i<$stop ? ',' : ']')."\n";  // conditionally add comma to end of row
    }
    return $result;
}

echo buildPseudoArray(1,2017);  // 0, 0, 0 in first column
echo "\n---------------------\n";
echo buildPseudoArray('2',2017);  // 0, 0, 0 in second column
echo "\n---------------------\n";
echo buildPseudoArray(12,2017);  // incrementation into next year

输出3个电话:

Result for 1-2017:
[['Day', 'Jan 2017', 'Feb 2017'],
['01.', '22', '9', ],
['02.', '30', '24', ],
['03.', '26', '5', ],
['04.', '46', '25', ],
['05.', '7', '24', ],
['06.', '17', '22', ],
['07.', '34', '27', ],
['08.', '0', '26', ],
['09.', '43', '38', ],
['10.', '27', '49', ],
['11.', '7', '22', ],
['12.', '14', '26', ],
['13.', '0', '30', ],
['14.', '45', '43', ],
['15.', '30', '45', ],
['16.', '34', '34', ],
['17.', '19', '30', ],
['18.', '34', '40', ],
['19.', '25', '39', ],
['20.', '1', '1', ],
['21.', '40', '49', ],
['22.', '26', '40', ],
['23.', '37', '44', ],
['24.', '16', '8', ],
['25.', '21', '3', ],
['26.', '47', '35', ],
['27.', '47', '25', ],
['28.', '1', '43', ],
['29.', '7', '30', ],
['30.', '8', '28', ],
['31.', '38', '13', ]]

---------------------
Result for 2-2017:
[['Day', 'Feb 2017', 'Mar 2017'],
['01.', '5', '48', ],
['02.', '43', '33', ],
['03.', '32', '13', ],
['04.', '30', '23', ],
['05.', '10', '41', ],
['06.', '3', '16', ],
['07.', '46', '23', ],
['08.', '3', '12', ],
['09.', '6', '23', ],
['10.', '48', '29', ],
['11.', '6', '49', ],
['12.', '14', '34', ],
['13.', '44', '2', ],
['14.', '17', '11', ],
['15.', '47', '44', ],
['16.', '35', '9', ],
['17.', '0', '26', ],
['18.', '26', '0', ],
['19.', '7', '5', ],
['20.', '0', '17', ],
['21.', '4', '9', ],
['22.', '43', '37', ],
['23.', '21', '28', ],
['24.', '22', '12', ],
['25.', '15', '50', ],
['26.', '16', '34', ],
['27.', '33', '21', ],
['28.', '2', '37', ],
['29.', '31', '41', ],
['30.', '50', '28', ],
['31.', '3', '34', ]]

---------------------
Result for 12-2017:
[['Day', 'Dec 2017', 'Jan 2018'],
['01.', '42', '48', ],
['02.', '17', '44', ],
['03.', '11', '26', ],
['04.', '5', '11', ],
['05.', '44', '1', ],
['06.', '37', '33', ],
['07.', '33', '32', ],
['08.', '43', '35', ],
['09.', '5', '35', ],
['10.', '27', '6', ],
['11.', '24', '0', ],
['12.', '35', '33', ],
['13.', '38', '4', ],
['14.', '11', '35', ],
['15.', '0', '18', ],
['16.', '3', '43', ],
['17.', '31', '20', ],
['18.', '12', '19', ],
['19.', '38', '34', ],
['20.', '38', '45', ],
['21.', '11', '32', ],
['22.', '12', '13', ],
['23.', '47', '38', ],
['24.', '37', '17', ],
['25.', '36', '44', ],
['26.', '47', '4', ],
['27.', '40', '48', ],
['28.', '5', '20', ],
['29.', '18', '4', ],
['30.', '15', '48', ],
['31.', '38', '19', ]]