In this sample code, is it possible to skip an element in the array, say, 3? If so, how? <?php $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { $value = $value * 2; } Thanks
<?php $arr = array(1, 2, 3, 4); foreach ($arr as $value) { if($value==3){ continue; } echo $value = $value * 2 . "\n"; } ?> Code (markup): returns: 2 4 8
this is one way: <?php $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { if($value!=3){ $value = $value * 2; } } ?> PHP: And here's another: <?php $arr = array(1, 2, 3, 4); foreach ($arr as &$value) { $i++; if($i!=3){ $value = $value * 2; } } ?> PHP: There are other ways if these are not what you're looking for, but you'll have to provide more details.