0

I have a user drop-down field that includes High, Medium, or Low ranking. Each ranking has three requirements for evaluation types. I also have three additional drop-downs with options to choose between those requirements, but they must meet the requirements in any order. I want to alert the user if they do not. Again, they do not need to be in the same order as the requirements.

// 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 Answer 1

1

Here’s one way to solve this:

  • sort both arrays
  • loop through them and compare them item by item (they should be equal, because arrays are sorted)

.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.