<?php
$array = array(a,s,d,f,g,h,j,k,l);
foreach($array as $i => &$a){
foreach($array as $k => &$b){
if($k = 4){
unset($array[1]);
}
}
echo $a . "\n";
}
print_r($array);
CODEPAD: http://codepad.org/UoWhrIkv
Why in this example echo show me only "a" and print_r show all good? Is possible to make good showing in loop with echo?
Two problems here:
The first is you’re using
=instead of==in this line:The second is your logic, as when your inner loop iterates over the same array, it will repeatedly unset
$array[1].That does not influence the inner foreach, but the outer one. So
echo $a;has a chance to print once only.Changing your
ifstatement to:Made the
echo $aprint the entire array (assuming that’s what you’re going for).