|
Posted by Schraalhans Keukenmeester on 05/19/07 04:52
At Fri, 18 May 2007 20:10:57 -0700, ZeldorBlat let his monkeys type:
>> ==================
>
> This suggests that it's safe (from <http://www.php.net/foreach>):
>
> "Unless the array is referenced, foreach operates on a copy of the
> specified array and not the array itself. Therefore, the array pointer
> is not modified as with the each() construct, and changes to the array
> element returned are not reflected in the original array."
>
Okay, well, uhmmm, not so well hidden then.
Try unsetting and/or changing values in a referenced array foreach:
You might not end u with what you expect. I repeated the code in my other
reply, now operating on $value directly:
$array=array('john','james','delilah','mary');
foreach ($array as $key=>&$value) {
if ($value == 'delilah') {
$value ='samson';
}
elseif ($value=='james') {
$value = null;;
}
echo "$key => $value".NEWLINE;
}
foreach ($array as $key=>$value) {
echo "$key => $value".NEWLINE;
}
Output:
0 => john
1 =>
2 => samson
3 => mary
0 => john
1 =>
2 => samson
3 => samson // !!!
Must be some logic for this, but I fail to see it right now.
Sh.
Navigation:
[Reply to this message]
|