How can I use an array in the case of a switch? This doesn’t work and always take the default (3):
switch ($my_array) {
case array('george','paul'):
$id = 1;
break;
case array('paul','max'):
$id = 2;
break;
case array('eric'):
$id = 3;
break;
//default
default:
$id = 3;
break;
}
Your example should work, according to the PHP manual on array operators:
Since switch/case uses weak comparison, arrays are compared by using the
==operator.I’ve put a working example onto codepad: http://codepad.org/MhkGpPRp