Re: Alter an Array Key
| From: | Robert Cummings | Date: | Sat, 14 Jul 2007 15:07:08 +0000 |
| Subject: | Re: Alter an Array Key | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-258864@lists.php.net to get a copy of this message | ||
On Sat, 2007-07-14 at 14:09 +0100, Stut wrote:
>
> > 3. I'm not sure Roberts solution would work. I think it might result
> > in an endless loop, and timeout your script.
>
> I always worry about adding or removing elements while iterating through
> an array. I generally build up an array of keys to remove and remove
> them in a separate loop afterwards just to be on the safe side.
I'm, not entirely sure, but I think foreach may grab a copy of the keys
before iteration. I've never worried about that in foreach although I
can remember issues back in the old days with the reset(), next(), end()
set of array traversal functions :)
If you're at all worried anyways, you can always do:
<?php
foreach( array_keys( $_POST ) as $key )
{
if( strpos( $key, '_' ) !== false )
{
$_POST[str_replace( '_', '', $key )] = $_POST[$key];
}
}
?>
Cheers,
Rob.
--
...........................................................
SwarmBuy.com - http://www.swarmbuy.com
Leveraging the buying power of the masses!
...........................................................