简体   繁体   English

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

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

How can I store dates on a multi-dimensional associative array? 如何将日期存储在多维关联数组上? I can't explain further my problem, but what if I have an array that contains different dates. 我无法进一步解释我的问题,但是如果我有一个包含不同日期的数组该怎么办。 For example: 例如:

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

I want to store the array above into multi dimensional associative array, according to their month, so therefore it would be like this.. 我想根据它们的月份将上面的数组存储到多维关联数组中,因此就像这样。

<?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")
);

?>

But in my case, the dates are from the database compare to the example above that are defined dates, how can I group that following dates according to their month and store in an associative array named according to their month and the content is the second dimensional array that contains the date grouped? 但就我而言,日期是来自数据库的,与上面定义日期的示例相比,我如何根据其月份将以下日期分组,并存储在根据其月份命名的关联数组中,而内容是第二维包含分组日期的数组?

Thanks! 谢谢!

My code above are just example: This is my solution but it's not working good! 上面的代码仅是示例:这是我的解决方案,但效果不佳! -_- -_-

<?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;
}
?>

your over thinking this, just use the month as the array key: 如果您对此有所考虑,只需将月份用作数组键:

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

?>

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

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

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