如何在php中打印与特定日期对应的特定金额

时间:2015-03-13 07:41:32

标签: php arrays codeigniter

我已经获取了具有以下结构的数组:

   Array
    (
    [record] => Array
    (
        [0] => Array
            (
                [amount] => 80
                [dates] => 12-Mar-2014
            )

        [1] => Array
            (
                [amount] => 80
                [dates] => 12-Mar-2015
            )

        [2] => Array
            (
                [amount] => 140
                [dates] => 13-Mar-2015
            )

        [3] => Array
            (
                [amount] => 80
                [month_year] => 03-2014
            )

        [4] => Array
            (
                [amount] => 220
                [month_year] => 03-2015
            )

    )

  )

在我看来,我只需要获取当天的金额(2015年3月13日)和当前月份金额,我不知道如何制定条件,如果月份是当前年度的当月,那么打印金额对应于那个数组中的月份,如果当天是当前年份的当天,那么只从数组中获取与该特定日期相对应的数量。

1 个答案:

答案 0 :(得分:2)

在循环内部使用if条件,还包括检查datesmonth_year的索引,如下所示:

foreach($array['record'] as $record) {
    $dates = date('d-M-Y');
    $month_year = date('m-Y');
    if(
        (isset($record['dates']) && $record['dates'] == $dates) ||
        (isset($record['month_year']) && $record['month_year'] == $month_year)
    ) {
        echo $record['amount'];
    } 
}