How can all selected values from a select element with multiple options be stored in an array in PHP?

To store all selected values from a select element with multiple options in an array in PHP, you can use the $_POST superglobal array to access the selected values. You can then loop through the selected values and store them in an array.

// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Initialize an empty array to store selected values
    $selectedValues = array();
    
    // Check if the select element has been submitted
    if(isset($_POST['selectElement'])){
        // Loop through the selected values and store them in the array
        foreach($_POST['selectElement'] as $value){
            $selectedValues[] = $value;
        }
    }
    
    // Print the array of selected values
    print_r($selectedValues);
}