检查多维中是否存在两对值和键


$permissions = array(    
    0 => array(  
        "action"   => "CREATE",  
        "subject"  => "USER"  
    ),  
    1 => array(  
        "action"   => "EDIT",  
        "subject"  => "USER"  
    ),   
    2 => array(  
        "action" =>  "DELETE",  
        "subject" => "USER"  
    ),
    3 => array(  
        "action"   => "CREATE",  
        "subject"  => "TASK"  
    ),  
);

我有这个代码来检查"键"one_answers"值"之间是否有组合。完美的工作。

    public static function verifyPermissions($array, $key, $val) {
    foreach ($array as $item){
        if ( $item[$key] == $val)
            return true;
    }
    return false;
}

下面一行代码返回"TRUE",但是在数组的同一行中没有"action = EDIT"one_answers"subject = TASK"的组合。

if(Group::verifyPermissions($permissions,"action","EDIT") && Group::verifyPermissions($permissions,"subject","TASK"))

我需要在数组的同一行中同时检查这两个选项。如何修复此错误?

您可以像这样展开您的函数

public static function verifyTwoPermissions($array, $keys, $values) {
    foreach ($array as $item){
        if ($item[$keys[0]] == $values[0] && $item[$keys[1]] == $values[1])
            return true;
    }
    return false;
}
// verifyTwoPermissions($permissions, array("action", "subject"), array("EDIT", "TASK"))

相关内容

  • 没有找到相关文章

最新更新