如果数组的值与另一个数组的值匹配,则

时间:2019-10-03 作者:Michael

我有一个用户下拉字段,包括高、中或低排名。每个排名对评估类型有三个要求。我还有三个额外的下拉列表,可以在这些需求之间进行选择,但它们必须以任何顺序满足需求。如果用户不这样做,我想提醒他们。同样,它们不需要与需求的顺序相同。

// Get requirements from Ranker field
if ($user->$currentRanking == \'High\'){
    $rankShouldBe1 = \'Standard Evaluation\';
    $rankShouldBe2 = \'Business Review & Coaching\';
    $rankShouldBe3 = \'Coaching Session Only\';
} elseif ($user->$currentRanking == \'Medium\'){
    $rankShouldBe1 = \'Standard Evaluation\';
    $rankShouldBe2 = \'Reduced Evaluation & Coaching\';
    $rankShouldBe3 = \'Coaching Session Only\';
} elseif ($user->$currentRanking == \'Low\'){
    $rankShouldBe1 = \'Standard Evaluation\';
    $rankShouldBe2 = \'Standard Evaluation\';
    $rankShouldBe3 = \'Reduced Evaluation & Coaching\';
} else {
    $rankShouldBe1 = \'-\';
    $rankShouldBe2 = \'-\';
    $rankShouldBe3 = \'-\';
}

// These are the two arrays I want to check if they match
$evaltypesReq = array(
    $rankShouldBe1,
    $rankShouldBe2,
    $rankShouldBe3,
);
$evaltypesSch = array(
    $user->pbe_evaltype_month1, // Option Field 1
    $user->pbe_evaltype_month2, // Option Field 2
    $user->pbe_evaltype_month3, // Option Field 3
);

// I have tried the following three methods with the If/Then statement below (obviously not at the same time):
$containsAllValues = array_intersect($evaltypesReq, $evaltypesSch) == $evaltypesReq;
$containsAllValues = 0 == count(array_diff($evaltypesReq, $evaltypesSch));
$containsAllValues = !array_diff($evaltypesReq, $evaltypesSch);

if ($containsAllValues == true){
    $results .=  \'Everything looks great!\';
} elseif ($containsAllValues == false){
    $results .=  \'This is not allowed.\';
} elseif ($containsAllValues == \'\'){
    $results .=  \'Please select the evaluation types you have scheduled.\';
} else {
    $results .=  \'\';
}

1 个回复
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成

有一种方法可以解决这个问题:

对两个数组进行排序循环遍历并逐项比较(由于数组已排序,因此它们应该相等)

    sort( $evaltypesReq );
    sort( $evaltypesSch );
    $containsAllValues = true;
    foreach ( $evaltypesReq as $k => $v ) {
        if ( $evaltypesSch[$k] !== $v ) $containsAllValues = false;
    }
    

相关推荐

5 PHP致命错误:未捕获ArgumentCountError(找不到)

我最近将WordPress站点从PHP5迁移到PHP7。我们的一个自定义文件列表功能(从服务器目录上的文件夹输出文件列表)在PHP7上的新服务器上失败。我知道存在争论冲突,但我不知道如何纠正它。The error:5 PHP致命错误:Uncaught ArgumentCounter错误:函数printDirectory()的参数太少,传入了3个/nas/content/live/lfccedu/wp-content/plugins/lfcc/lfcc。第1026行的php和/nas/content/liv