I have an array.
$arrayVar = array(8, 10, 'u'=>24, 'm'=>45, 54, 45);
foreach($arrayVar as $curK=>$curV)
switch($curK)
{
case 'u':
echo $curK; //statement
break;
default:
//statements
}
Why are the statements under the first case getting executed for all $curK where the array key has not been specified apart from when the label matches the value of $curK?
I did try overcoming the problem by checking $curK for empty values
if(!empty($curK)) {
foreach($arrayVar as $curK=>$curV)
switch($curK)
{
case 'u':
echo $curK; //statement
break;
}
}
...
$arrayVar = array('a'=>8400,'u'=>1100,1300,1400,true);
echo '<pre>'; print_r($arrayVar);
foreach($arrayVar as $curK=>$curDU)
{
echo "\n" . $curK . "\t";
switch($curK)
{
case 'a':
echo 'When a : ' . $curK . "\n";
break;
case 'u':
echo 'When u : ' . $curK . "\n";
break;
default:
}
}
This is the result i got
Array ( [a] => 8400 [u] => 1100 [0] => 1300 [1] => 1400 [2] => 1 )
a When a : a
u When u : u
0 When a : 0
1
2
The problem arose with the php assigned key 0.