我有一个用户下拉字段,包括高、中或低排名。每个排名对评估类型有三个要求。我还有三个额外的下拉列表,可以在这些需求之间进行选择,但它们必须以任何顺序满足需求。如果用户不这样做,我想提醒他们。同样,它们不需要与需求的顺序相同。
// 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 .= \'\';
}
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成
有一种方法可以解决这个问题:
对两个数组进行排序循环遍历并逐项比较(由于数组已排序,因此它们应该相等)
sort( $evaltypesReq );
sort( $evaltypesSch );
$containsAllValues = true;
foreach ( $evaltypesReq as $k => $v ) {
if ( $evaltypesSch[$k] !== $v ) $containsAllValues = false;
}