What role can arrays play in organizing sorting options in PHP?

Arrays can play a crucial role in organizing sorting options in PHP by allowing us to store different sorting criteria as elements of an array. This can make it easier to dynamically select and apply sorting options based on user input or specific requirements. By using arrays, we can easily iterate through the sorting options and apply the desired sorting algorithm accordingly.

// Define an array of sorting options
$sortingOptions = [
    'name' => 'Sort by Name',
    'date' => 'Sort by Date',
    'price' => 'Sort by Price'
];

// Example of applying sorting based on user input
$userInput = 'name'; // Assume user selects 'Sort by Name'
if (array_key_exists($userInput, $sortingOptions)) {
    $selectedOption = $sortingOptions[$userInput];
    // Apply sorting based on $userInput
    echo "Sorting selected: $selectedOption";
} else {
    echo "Invalid sorting option selected";
}