创建阵列/收藏/的(日期,金额)在PHP列表 [英] Create an array/collection/list of (Date,Value) in PHP

查看:123
本文介绍了创建阵列/收藏/的(日期,金额)在PHP列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个PHP脚本,输入是:

I am writing a PHP script where the inputs are:

From date
To date

然后我想利用这个日期范围,并建立某种形式的有数组:

I then want to take that date range and create an array of some sort that has:

Array(date,x)

当我每个日期添加到数组,我将计算连同它的价值。

As I add each date to the array, I will calculate the value that goes with it.

使用.NET我会(把我的头顶部)使用一本字典其中的日期是关键,x是值。

With .NET I would (off the top of my head) use a dictionary where the date was the key and x is the value.

的主要任务是,到范围内某种类型的数组或字典得到该日期范围,然后拆分(什么是在PHP理想)

The main task is getting that date range, then splitting that range into an array or dictionary of some sort (What would be ideal in php)

因为我将它添加到阵列我会运行一些其他的code,我已经写了计算去与该日期值

As i'm adding it to the array i'll run off some other code i've already wrote that calculate the value to go with that date

在这一切结束的时候它在那里的补充,我需要通过数组或列表进行迭代,并添加所有的x值在一起。

At the end of it all when it's added in there, i'll need to iterate through the array or list and add all the x values together.

推荐答案

你可以试试这个


function makeDateRange($from,$to,$pattern='m-d-y')
{
    $day = date( $pattern , $from );
    $to = date( $pattern , $to );
    $parseDate = date_parse($from);
    while( $day <= $to ) {
        $day = mktime(0,0,0,$parseDate["month"],$parseDate["day"]+1,$parseDate["year"]);
        $dateArray[] = date($pattern , $day);
        }
    return $dateArray;
}

//这里做阵列
$键= makeDateRange(11年12月1日,11年12月2日);

// here make array $keys = makeDateRange("12-01-11","12-02-11");

//这里对上面数组作为在$ a数组的键
$ a = array_fill_keys($键,'无');
的print_r($ A);

//here make above array as key in $a array $a = array_fill_keys($keys, 'none'); print_r($a);

这篇关于创建阵列/收藏/的(日期,金额)在PHP列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆