Switch and break; inside a foreach loop

Hello,

I’m using a swtich inside a foreach loop. When I break; in the switch contect, it seems to break the loop.

Is there a trick I ignore? I read the comments in the manual, nothing helped me… :slight_smile:

Regards,

-jj.

Post the script please

Break lets you specify the number of control structures to break out of. If you do a

break 1;

that should jump out of just the switch statement. I haven’t tried it but that is what the manual says.

break 2;

should break you out of the switch and the loop.

Do you have a code sample?

This seems to work OK.


<?php
for( $iCounter = 1 ; $iCounter <= 5 ; $iCounter++ )
{
    switch ($iCounter)
    {
        case 1:
            echo 'One';
        break;
        
        case 2:
            echo 'Two';
        break;
        
        case 3:
            echo 'Three';
        break;
        
        case 4:
            echo 'Four';
        break;
        
        case 5:
            echo 'Five';
        break;
    }
    echo "<br />\\r\
";
}
?>

Output

One<br />
Two<br />
Three<br />
Four<br />
Five<br />

Looks like it works to me. It prints the texct version of the number and jumps out of the switch statement and echos the <br> statement and then goes to the next iteration of the loop. I don’t understand why you think it is breaking the loop. Try it with 7 instad of 5. you will get 5 text number with <BR> and then you will get two lines (for 6 and 7) of just the <br> stuff.

This is not the OP’s code. :wink:

Looks good to me too, what licence is it covered by? :wink:

Thanks for your replies :slight_smile:

Sorry, I thought the code would be needless.


$load = array();

     $test = array();
     $test[] = 'hi';
     $test[] = 'hello';
     $test[] = 'yipee';
     $test[] = 'heya';
     $test[] = 'soCool';

     foreach($test as $k => $v){

       switch ($v){

          case 'hi':
            $load['encounter'] = '';
            break;
          case 'hello':
            $load['encounter']['hello'] = $v;
            break;
          case 'yipee':
            $load['encounter']['hello']['yipee'] = $v;
            break;
          case 'heya':
            $load['encounter']['hello']['heya'] = $v;
            break;
          case 'soCool':
            $load['encounter']['hello']['soCool'] = $v;

       }
     }

     echo('<pre>');
     print_r($load);
     echo('</pre>');
     die();

I am not sure what you are trying to achieve in your example but your array structure in the switch case is ****ed up.
If you are trying to put every string in the load array in own cell you would do this for example:


<?php
$load = array();
$test = array();
$test[] = 'hi';
$test[] = 'hello';
$test[] = 'yipee';
$test[] = 'heya';
$test[] = 'soCool';

foreach($test as $k => $v){
	switch ($v){
		case 'hi':
			$load[] = '';
			break;
		case 'hello':
			$load[] = $v;
			break;
		case 'yipee':
			$load[] = $v;
			break;
		case 'heya':
			$load[] = $v;
			break;
		case 'soCool':
			$load[] = $v;
			break;
	}
}
echo('<pre>');
print_r($load);
echo('</pre>');
die(); 

Will output this:


Array
(
    [0] => 
    [1] => hello
    [2] => yipee
    [3] => heya
    [4] => soCool
)

What result does the print_r give?

Your problem is in your array structure. The looping is all working fine. I don’t know the technical details behind it all but here is my guess.

You define $load as a 2 dimensional array when you assign $v in this line:

$load['encounter']['hello'] = $v;

Once that is done it doesn’t look like you can make it into a 3 dimensional array as you try to do in the other lines which makes sense. If you change the line to this:

$load['encounter']['hello']['hello'] = $v;

then it works. You are using a 3 dimensional array throughout.