try this: <?php echo "foo".$i++; echo "\n"; echo "bar".($+++0); ?> Thanks for showing that I add new element to the array. This is because on last iteration $k is null and I access $ar[$k] which automatically adds the new element. Why? I don't know? Maybe because I refer it by reference! The fixed code is
<pre> <?php for ($i=0;$i<10;$i++) $ar['foo'.$i] = 'bar'.$i; var_dump($ar); for(reset($ar),list($k,)=each($ar),$v=&$ar[$k];$k;list($k,)=each($ar),($k)?$v=&$ar[$k]:''){ var_dump($k,$v); $v='ather'.($j+++0); var_dump($k,$v); } var_dump($ar); ?> This will not add new element to the array. You ask why I did that. First to say that I wanted to make something really weird to happy my day :)) Second I wanted to implement a feature from PHP's(Zend's) ToDo list. Third think about if the array is so big (some megabytes) since I hold really big strings in every $v. Then if I use the standart foreach() statement PHP will have to copy $ar[$k] to $v :))) Best regards and fun with PHP, Andrey Hristov On Thursday 07 March 2002 08:48 pm, you wrote: > On Thu, 2002-03-07 at 09:53, Andrey Hristov wrote: > > The code below is too weird. I spent 15 min writing it but now works: > > > > for(reset($ar),list($k,)=each($ar),$v=&$ar[$k];$k;list($k,)=each($ar),$v= > >&$ar[$k]){ var_dump($k,$v); > > $v='ather'.($j+++0); > > var_dump($k,$v); > > } > > > > The $ar array is generated in this way : > > > > for ($i=0;$i<10;$i++){ > > $ar['foo'.$i] = 'bar'.$i; > > } > > Hi there, > > I'm not sure exactly what the above code is for--but it seems to be > going to an awful lot of trouble to do it. :) If I start with the array: > > Array > ( > [foo0] => bar0 > [foo1] => bar1 > [foo2] => bar2 > [foo3] => bar3 > [foo4] => bar4 > [foo5] => bar5 > [foo6] => bar6 > [foo7] => bar7 > [foo8] => bar8 > [foo9] => bar9 > ) > > ...and run the first for() loop above over it, I get this: > > Array > ( > [foo0] => ather0 > [foo1] => ather1 > [foo2] => ather2 > [foo3] => ather3 > [foo4] => ather4 > [foo5] => ather5 > [foo6] => ather6 > [foo7] => ather7 > [foo8] => ather8 > [foo9] => ather9 > [] => > ) > > Note the extra null element at the end. > > Why not just do this? > > $j = 0; > foreach ($ar as $key => $val) { > $ar[$key] = 'ather' . $j++; > } > > ...which results in: > > Array > ( > [foo0] => ather0 > [foo1] => ather1 > [foo2] => ather2 > [foo3] => ather3 > [foo4] => ather4 > [foo5] => ather5 > [foo6] => ather6 > [foo7] => ather7 > [foo8] => ather8 > [foo9] => ather9 > ) > > Also, I'm unsure what you're going for with the ($j+++0). It just > increments $j and adds 0, which doesn't seem to do anything. > > Please forgive me if I have misunderstood. > > > Cheers, > > Torben > > > Best regards, > > Andrey Hristov > > > > P.S. > > There is in PHP's ToDo list such feature. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php