gpt4 book ai didi

php - 检查一个数组中的任何项目是否包含在另一个数组中的任何项目中

转载 作者:行者123 更新时间:2023-12-04 08:07:20 25 4
gpt4 key购买 nike

我有一系列订阅/计划(访问级别):

define('PLAN_A', 0); // 0001    
define('PLAN_B', 2); // 0010
define('PLAN_C', 4); // 0100
define('PLAN_D', 8); // 1000
define('PLAN_E', 16); // 10000
一个用户可以订阅一个或多个计划。
$user_one = array(PLAN_A, PLAN_C); // is subscribed to two plans
$user_two = array(PLAN_C); // is only subscribed to one plan
“流程”需要特定的订阅计划级别 - 或多个计划级别:
$process_1 = array(PLAN_B); // requires only PLAN_B subscripton
$process_2 = array(PLAN_B, PLAN_D); // requires either PLAN_B or PLAN_C subscription
我想检查是否 $user_one有“权限”访问 $process_1 , 并分别检查是否 $user_one有权访问 $ process_2 .并对 $user_two 做同样的检查. (“权限”是指用户是否拥有流程所需的订阅计划。)
看起来我需要检查流程的订阅要求中是否包含任何用户计划订阅(一个或多个)。
我尝试使用按位检查(这就是 PLAN 具有二进制值的原因),但这仅适用于检查 $process_1 .我如何检查 $user_1可以访问 $process_2 ?或者,如何检查用户数组的任何值是否包含在流程需求数组的任何值中?

最佳答案

由于其中一个用户 ( jirarium ) 复制了我的评论并将其作为答案发布,我不妨自己做,并实际获得我的代码段的功劳,而不是让那些只是复制和粘贴其他人的代码声称它是他自己的。
这是我昨天做的功能。您输入用户和进程;然后它将遍历所有用户值(或本例中的计划)并检查它们中的任何一个是否包含在您包含的过程值数组中。基本上检查用户数组的任何值是否包含在流程需求数组的任何值中,这正是您想要的。
它将返回可用于 if 的真或假。陈述

<?php
define('PLAN_A', 1); // 0001 <-- corrected
define('PLAN_B', 2); // 0010
define('PLAN_C', 4); // 0100
define('PLAN_D', 8); // 1000
define('PLAN_E', 16); // 10000
define('PLAN_F', 32); // 10000

$user_one = array(PLAN_A, PLAN_C);
$user_two = array(PLAN_C);
$user_three = array(PLAN_F, PLAN_D);

$process_1 = array(PLAN_B);
$process_2 = array(PLAN_B, PLAN_D);

function check($user, $process){
foreach($user as $pl){
if(in_array($pl, $process)){
return true;
}
}
return false;
}
用法:
    if(check($user_one, $process_1)){
echo 'true';
}else{
echo 'false';
}
现场演示: http://sandbox.onlinephpfunctions.com/code/83d8d135b03d584713738b56db40ecc21e9f4ddd

关于php - 检查一个数组中的任何项目是否包含在另一个数组中的任何项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66165957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com