note 90323 modified in control-structures.foreach by danbrown
| From: | danbrown@php.net | Date: | Thu, 16 Apr 2009 13:08:19 +0000 |
| Subject: | note 90323 modified in control-structures.foreach by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153387@lists.php.net to get a copy of this message | ||
Using continue and break:
<?php
$stack = array('first', 'second', 'third', 'fourth',
'fifth');
foreach($stack AS $v){
if($v == 'second')continue;
if($v == 'fourth')break;
echo $v.'<br>';
}
/*
first
third
*/
$stack2 = array('one'=>'first', 'two'=>'second',
'three'=>'third', 'four'=>'fourth',
'five'=>'fifth');
foreach($stack2 AS $k=>$v){
if($v == 'second')continue;
if($k == 'three')continue;
if($v == 'fifth')break;
echo $k.' ::: '.$v.'<br>';
}
/*
one ::: first
four ::: fourth
*/
?>
--was--
Using continue and break:
<?php
$stack = array('first', 'second', 'third', 'fourth',
'fifth');
foreach($stack AS $v){
if($v == 'second')continue;
if($v == 'fourth')break;
echo $v.'<br>';
}
/*
first
third
*/
$stack2 = array('one'=>'first', 'two'=>'second',
'three'=>'third', 'four'=>'fourth',
'five'=>'fifth');
foreach($stack2 AS $k=>$v){
if($v == 'second')continue;
if($k == 'three')continue;
if($v == 'fifth')break;
echo $k.' ::: '.$v.'<br>';
}
/*
one ::: first
four ::: fourth
*/
?>
http://php.net/manual/en/control-structures.foreach.php