What are the potential pitfalls of using switch case statements for dynamic sorting in PHP?

Using switch case statements for dynamic sorting in PHP can become cumbersome and difficult to maintain as the number of sorting options increases. A more scalable and flexible approach would be to use an associative array where the keys represent the sorting options and the values represent the sorting logic or functions. This way, adding or modifying sorting options can be easily done without having to update the switch case statements.

// Define an associative array for dynamic sorting
$sortingOptions = [
    'name' => function($a, $b) {
        return strcmp($a['name'], $b['name']);
    },
    'age' => function($a, $b) {
        return $a['age'] - $b['age'];
    },
    'date' => function($a, $b) {
        return strtotime($a['date']) - strtotime($b['date']);
    }
];

// Sort the array based on the selected sorting option
usort($data, $sortingOptions[$selectedSortingOption]);