繁体   English   中英

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

[英]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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM