What are some potential pitfalls of using multiple nested switch statements in PHP code?

Using multiple nested switch statements can make the code harder to read and maintain. It can also lead to code duplication and make it difficult to add new cases or modify existing ones. To solve this issue, consider refactoring the code to use a different control structure, such as arrays or objects, to handle the logic more efficiently.

// Example of refactoring nested switch statements using an associative array

$cases = [
    'case1' => function() {
        // code for case 1
    },
    'case2' => function() {
        // code for case 2
    },
    'default' => function() {
        // default case
    }
];

$selectedCase = 'case1'; // set the selected case dynamically

if (array_key_exists($selectedCase, $cases)) {
    $cases[$selectedCase]();
} else {
    $cases['default']();
}