Search code examples
phparraysvariablescountdefault

Set default output value if frequency of all input elements is the same


Good day all, I hardly ever need to ask here because searching always yields the answers I am looking but this time I just don't know what to search for.

The following code works as expected and shows the desired result.

Note: There can always be only 3 results, namely "Average", "Above average" or "Below average"; example:

$test1 = 'Average';
$test2 = 'Above average';
$test3 = 'Average';

$mystuff = array($test1, $test2, $test3);
$result = array_count_values($mystuff);
asort($result);
end($result);
$answer = key($result);
echo 'Averaging : ' . $answer;

Working Example:

Input: $test1='Average' , $test2='Above average' , $test3='Average'

Expected output: Averaging : Average (This is correct because it is the most frequent occurrence in the array.)

The problem is, when all three $test variables are different :

   $test1 = 'Above average';
   $test2 = 'Below average';
   $test3 = 'Average';

Now, $answer = Above average (ie. the first occurrence in the array).

However, I would like to set a default value when there is no "frequent occurrence".

What I would like to have is this:

If all three variables are different, then $answer must ALWAYS show 'Average', regardless of $test1 or $test2 or $test3 values.

I don't expect you to give the code I need, just point my in the right direction as to what to search for.


Solution

  • Method:

    $result = array_count_values($mystuff);
    if(sizeof($result)==1){                         // only one unique value
        $answer = key($result);                     // get lone element's value
    }elseif(sizeof(array_flip($result))==1){        // if all value counts are same
        $answer = 'Average';                        // set default value
    }else{
        arsort($result);                            // sort DESC
        $answer = key($result);                     // get first element's value
    }
    echo "Averaging : $answer";
    

    Inputs/Outputs:

    $mystuff=['Average','Below Average','Below Average'];        // Averaging : Below Average
    $mystuff=['Average','Above Average','Below Average'];        // Averaging : Average
    $mystuff=['Above Average','Above Average','Above Average'];  // Averaging : Above Average
    

    Just some notes, I have opted for array_flip() over array_unique() because it is a faster performer and it provides a result that is equally useful.

    I could have written my method with just two conditions like:

    $result = array_count_values($mystuff);
    if(sizeof($result)>1 && sizeof(array_flip($result))==1){
        $answer = 'Average';                        // set default value
    }else{
        arsort($result);                            // sort DESC
        $answer = key($result);                     // get first element's value
    }
    echo "Averaging : $answer";
    

    However, that would mean that arsort() could be senselessly sorting a one-element array. I mean, it's no big deal -- especially because arsort() is fast and your array is so small. So, I guess what I am saying is, if code brevity is important to you, use this alternative method.