Php 全年适用';将日期作为键放入数组中
我有一个364个值的数组。数组的每个值表示一年中每一天的状态 该数组如下所示:Php 全年适用';将日期作为键放入数组中,php,arrays,Php,Arrays,我有一个364个值的数组。数组的每个值表示一年中每一天的状态 该数组如下所示: array([0]=>'something', [1]=>'something_else' .... [364]=> 'the_end_of the year') array([2015-01-01]=>'something', [2015-01-02]=>'something_else' .... [2015-1
array([0]=>'something',
[1]=>'something_else'
....
[364]=> 'the_end_of the year')
array([2015-01-01]=>'something',
[2015-01-02]=>'something_else'
....
[2015-12-31]=> 'the_end_of the year')
我要做的是将所有的数组键
替换为一年中的日期。看起来像这样的东西:
array([0]=>'something',
[1]=>'something_else'
....
[364]=> 'the_end_of the year')
array([2015-01-01]=>'something',
[2015-01-02]=>'something_else'
....
[2015-12-31]=> 'the_end_of the year')
也许你可以用这样的东西
<?php
date_default_timezone_set('UTC');
// Start date
$date = '2015-1-1';
// End date
$end_date = '2015-12-31';
$result = array();
while (strtotime($date) <= strtotime($end_date)) {
$date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
$result[$date]="your info";
}
echo "<pre>";print_r($result);
?>
好,下面是一个例子
$new = array();
foreach($array as $i=>$value) {
$key = date('Y-m-d',strtotime('01.01.2015 +'.$i.' days'));
$new[$key] = $value;
}
var_dump($new);
die;
您可以使用如下createFromFormat方法遍历数组并将当前日期转换为DateTime对象
$begin = new DateTime('2015-01-01');
$end = new DateTime('2015-12-31');
$end = $end->modify('+1 day');
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
$new = array();
foreach($daterange as $date){
$new[$date->format("Ymd")] = "Your values";
}
其中110是一年中的某一天,2015年是创建日期的年份。从那里,您应该能够将按键设置为您喜欢的任何格式。您可以尝试使用-
foreach可以成为你的朋友:)是的,我知道!但是我不知道怎么玩约会!