Current Location: Home> Latest Articles> How to use the PHP function array_count_values ​​to count the selection frequency of form options?

How to use the PHP function array_count_values ​​to count the selection frequency of form options?

M66 2025-05-16

In web development, counting the frequency of selection of form options is a common task. Whether it is a questionnaire, a scoring system, a selection box, etc., developers usually need to count which options the user has selected in the form and the number of times each option has been selected. The array_count_values ​​function in PHP can easily accomplish this task. This article will introduce how to use this function to count the frequency of form options selection.

1. What is the array_count_values ​​function?

array_count_values ​​is a built-in function in PHP that counts the frequency of occurrence of all values ​​in an array and returns an associative array where the key is the value in the array and the value is the number of occurrences of each value.

Function syntax:

 array_count_values(array $array): array
  • $array : This is the array to be counted.

Return value:

The function returns an associative array where the key is the values ​​in the input array, and the values ​​are the number of times these values ​​appear in the array.

2. Example: Form Option Selection Frequency Statistics

Suppose we have a form where the user can choose from multiple options. We collected these options and wanted to count the number of times each option was selected.

2.1 Form Structure

Assuming our form contains a radio box, the user can choose different colors as their preferences. The HTML form code is as follows:

 <form action="process.php" method="POST">
    <label>Please select your preferred color:</label><br>
    <input type="radio" name="color" value="red"> red<br>
    <input type="radio" name="color" value="blue"> blue<br>
    <input type="radio" name="color" value="green"> green<br>
    <input type="submit" value="submit">
</form>

2.2 Simulate form data

In practical applications, form data is usually submitted through the POST method. To simulate this process, we will manually create an array containing the form selection results. Suppose the form data submitted by the user is as follows:

 $submitted_data = ['red', 'blue', 'green', 'red', 'blue', 'blue', 'green', 'red'];

2.3 Use array_count_values ​​function to select frequency

We can use the array_count_values ​​function to count the frequency of each color being selected:

 $selection_count = array_count_values($submitted_data);

print_r($selection_count);

Output result:

 Array
(
    [red] => 3
    [blue] => 3
    [green] => 2
)

Through the output results, we can clearly see that the selection frequency of each color: red and blue are selected 3 times each, and green is selected 2 times each.

3. Store form data to the database and count frequency

If your form data comes from a database and you want to count based on records in the database, array_count_values ​​can also help you achieve this.

Suppose you have a table that stores the color information selected by the user, and each user's selection is stored in a field in the database. You can query all selected data and then use the array_count_values ​​function to count the selection frequency of each option.

3.1 Database query example

 $servername = "localhost";
$username = "username";
$password = "password";
$dbname = "form_data";

// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check the connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT color FROM form_submissions";
$result = $conn->query($sql);

$submitted_data = [];

if ($result->num_rows > 0) {
    // Output each line of data
    while($row = $result->fetch_assoc()) {
        $submitted_data[] = $row['color'];
    }
} else {
    echo "0 result";
}

$conn->close();

3.2 Use array_count_values ​​function for statistics

After obtaining the data, use array_count_values ​​to count the selection frequency of each color:

 $selection_count = array_count_values($submitted_data);

print_r($selection_count);

4. Summary

array_count_values ​​is a very useful PHP function that helps developers quickly count the frequency of form options selection. Whether it is data submitted directly from the form or data extracted from the database, it can be easily achieved using array_count_values . Through the analysis of statistical results, developers can obtain valuable information and provide support for decision-making.