根据另一个key =>值的值获取数组键值=>的值?

时间:2014-09-12 20:06:46

标签: php arrays loops

我必须使用钥匙,' payment_date'并且平衡了#39;我有一个功能,检查' payment_date'看它是否< ='今天',它有效,但现在我不知道如何取得平衡'从那个:

function getCurrentBalance($myTable){
    $today = new DateTime('now');
    $today = $today->format('Y-m-d');
    foreach($myTable as $row) {
         foreach($row as $key=>$value) {
                 if ($key == "payment_date" && $value <= $today){

                 }
         }
     }
}

3 个答案:

答案 0 :(得分:6)

如果您已经知道所需的密钥,那么您真的不需要第二个循环。您可以直接访问它们。

function getCurrentBalance($myTable){
    $today = new DateTime('now');
    $today = $today->format('Y-m-d');
    foreach($myTable as $row) {
         if ($row['payment_date'] <= $today){
             //Do something with $row['balance']
         }
     }
}

答案 1 :(得分:0)

这样做:

foreach($myTable as $row) 
{
    if ($row['payment_date'] <= $today)
    {
        echo $row['balance'];                        
    }
}

答案 2 :(得分:0)

我不太清楚你想做什么,我认为你的答案是:

function getCurrentBalance($myTable){
    $today = new DateTime('now');
    $today = $today->format('Y-m-d');
    foreach($myTable as $row) {
        foreach($row as $key=>$value) {
            if ($key == "payment_date" && $value <= $today){
                $balance = $row['balance'];
                ...
            }
        }
    }
}