如何在多维关联数组中存储日期?


How can I store dates on a multi-dimensional associative array?

如何在多维关联数组中存储日期?我不能进一步解释我的问题,但如果我有一个数组,其中包含不同的日期。例如:

<?php 
$dates = array("2015-06-01","2015-06-07","2015-07-08","2015-08-01", "2015-08-21","2015-08-26");
?>

我想把上面的数组存储成多维关联数组,根据他们的月份,所以它应该是这样的…

<?php
array = array(
"June" => array("2015-06-01","2015-06-07"),
"July"=> array("2015-07-08"),
"August" => array("2015-08-01","2015-08-21","2015-08-26")
);
?>

但是在我的例子中,日期来自数据库,与上面定义的日期相比,我如何根据月份对以下日期进行分组并存储在根据月份命名的关联数组中,而内容是包含分组日期的第二维数组?

谢谢!

我上面的代码只是例子:这是我的解决方案,但它工作不好!_——

<?php 
include($_SERVER["DOCUMENT_ROOT"] . "/empc/library/functions.php");
$previousmonth = "";
$a = getDistinctDates(); // Function that get dates, nevermind of this.
$data = array();
foreach ($a as $b){ 
    echo $b["date_modified"] . "<br>";
    $datemonth = date("F",strtotime($b["date_modified"]));
    echo $datemonth . "<br>";
    if ($datemonth != $previousmonth){
        array_push($data,
            array(
                $datemonth => $b["date_modified"]
            )
        );
    } else {
        array_push($data[$datemonth][],$b["date_modified"]);
    }
    echo $b["balance_after_approval"] . "<br>";
    echo "<br>";
    $previousmonth = $datemonth;
}
?>

不要想太多,直接使用月份作为数组键:

<?php 
$dates = array("2015-06-01","2015-06-07","2015-07-08","2015-08-01", "2015-08-21","2015-08-26");
$out=array();
foreach ($dates as $b){ 
    $datemonth = date("F",strtotime($b));
    $out[$datemonth][]=$b;
}
  print_r($out);
?>

演示:http://codepad.viper - 7. - com/3gh9s7