if else语句不能按照我想要的方式在php中工作

时间:2015-03-14 06:06:30

标签: php codeigniter

我有以下代码

<?php  

foreach($record as $rec) {
    $dates = date('d-M-Y');
    $month_year = date('m-Y');

    if (($rec['dates'] == $dates && $rec['eid'] == $uid)) {
        $today = $rec['amount'];
    }

    if (($rec['dates']  == $month_year  && $rec['eid'] == $uid )) {
        $monthlysale = $rec['amount'];
    }
}

?>

我正在使用foreach循环,我的记录数组具有以下结构

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

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

[2] => Array
    (
        [amount] => 380
        [dates] => 13-Mar-2015
        [eid] => 63
    )

[3] => Array
    (
        [amount] => 160
        [dates] => 14-Mar-2015
        [eid] => 63
    )

[4] => Array
    (
        [amount] => 80
        [dates] => 03-2014
        [eid] => 63
    )

[5] => Array
    (
        [amount] => 620
        [dates] => 03-2015
        [eid] => 63
    )

我正在申请if条件来检查我从数组$ rec ['dates']获得的日期是否等于$ date变量中的今天日期,但如果今天的日期与日期不相符,则会出现问题数组然后变量$今天变得未定义,因为没有对应于该日期的金额,因为如果没有日期那么没有金额我不知道如何在foreach循环中使条件成为$ today = 0如果$ rec ['dates'](来自数组的日期)不等于今天的日期,第二种情况是如果有匹配的日期那么如果我应用其他条件我定义$ today = 0然后只有其他部分works.thanx in预先

1 个答案:

答案 0 :(得分:1)

在foreach循环之前将两个变量定义为默认值(零)。

$today = 0;
$monthlysale = 0;

foreach (...) {
    ...
}

// here both variables are defined, you can work with them.