'PHP - check a form for duplicate values and display a warning message

I have an HTML form with a number of statements which the user can rank as 1, 2, 3 or 4. I group the statements together in clusters of 4 and the ranks should only be used once per cluster (for example 4,3,2,1 not 1,1,2,3 etc.).

I need some PHP code that will check each cluster of 4 answers and display an error message if it finds any duplicates.

This is the 1st cluster in my form (written for Contact Form 7, hence the square brackets)

1. I like pasta
[select* one-01 first_as_label class:cluster1 "--" "1" "2" "3" "4"]

2. I like potato
[select* one-02 first_as_label class:cluster1 "--" "1" "2" "3" "4"]

3. I like rice
[select* one-03 first_as_label class:cluster1 "--" "1" "2" "3" "4"]

4. I like fries
[select* one-04 first_as_label class:cluster1 "--" "1" "2" "3" "4"]

HTML:

1. I like pasta
<select name="one-01" class="cluster1" ><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select>

2. I like potato
<select name="one-02" class="cluster1"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select>
    
3. I like rice
<select name="one-03" class="cluster1"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select>

4. I like fries
<select name="one-04" class="cluster1"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select>

I'm very new to PHP and I don't know the best way to treat the answers of a cluster of values as an array. (This is the best I could do. This is added to my child theme functions.php)

// Function to test for duplicate form entries
if(count(array_unique($cluster1))<count($cluster1))
{
    // Array has duplicates
    echo "You must only use each score once per cluster";
}
else
{
    // Array does not have duplicates
}

I think this php check array value for duplicate is the closest to what I'm trying to do, but I don't know how to treat all values of the same class as an array. I think there many be other errors in my implementation too.

All advice will be gratefully received by this novice!

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source