简体   繁体   English

获取基于另一个键=>值的数组键=>值的值?

[英]Get value of an array key=>value based on value of another key=>value?

I have to keys, 'payment_date' and 'balance'. 我必须输入“ payment_date”和“ balance”键。 I have a function that checks 'payment_date' to see if it's <= 'today', and it works but now I don't know how to fetch 'balance' from that: 我有一个检查'payment_date'以查看它是否<='today'的函数,它可以工作,但是现在我不知道如何从中获取'balance':

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){

                 }
         }
     }
}

You really don't need the second loop, if you already know the keys you need. 如果您已经知道所需的键,则实际上不需要第二个循环。 You can access them directly. 您可以直接访问它们。

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']
         }
     }
}

do it like this instead: 改为这样做:

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

I don't understand very well what do you want to do, I think that your answer is: 我不太了解您想做什么,我想您的答案是:

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'];
                ...
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM