Re: Alter an Array Key

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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!
...........................................................

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux