Re: [PHP] Alter an Array Key
On Sat, July 14, 2007 8:09 am, Stut wrote: Craige Leeder wrote: 1. Don't modify $_POST Why not? Other portions of a web application will too easily be confused by what you've modified as the application grows. Far better to have your clean data in a different array than to go poking altered content into $_POST, imho. -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Alter an Array Key
Craige Leeder wrote: 1. Don't modify $_POST Why not? 2. You controll the name of the array keys with the form. Why is there any need to change them form PHP's side of things? That's an assumption. A reasonable one in most cases, but not necessarily the case. 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. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Alter an Array Key
On Sat, 2007-07-14 at 01:57 -0400, Craige Leeder wrote: 3. I'm not sure Roberts solution would work. I think it might result in an endless loop, and timeout your script. It works fine. 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
Re: [PHP] Alter an Array Key
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
[PHP] Alter an Array Key
Hello, Is there any easy way to alter a key's name? I would like to remove some underscores in the $_POST array keys. Thanks, OD -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Alter an Array Key
On Fri, 2007-07-13 at 15:36 -0500, OD wrote: Hello, Is there any easy way to alter a key's name? I would like to remove some underscores in the $_POST array keys. ?php foreach( $_POST as $key = $value ) { if( strpos( $key, '_' ) !== false ) { $_POST[str_replace( '_', '', $key )] = $value; unset( $_POST[$key] ); } } ? I would suggest NOT removing the original key though and just have both. To do so just comment out/remove the line that has unset(). 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
Re: [PHP] Alter an Array Key
OD wrote: Hello, Is there any easy way to alter a key's name? I would like to remove some underscores in the $_POST array keys. Thanks, OD sure use str_replace in the value of the key name string. But, that probably doesn't answer your question. Why don't you supply us with an example of how you are using the $_POST array and maybe we can give you a better solution. -- Jim Lucas Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them. Twelfth Night, Act II, Scene V by William Shakespeare -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Alter an Array Key
On Fri, July 13, 2007 3:36 pm, OD wrote: Hello, Is there any easy way to alter a key's name? I would like to remove some underscores in the $_POST array keys. Not directly, but: $post = array(); foreach($_POST as $k = $v){ $k = str_replace('_', ' ', $k); $post[$k] = $v; } You could dink with unset instead, but it's a Bad Idea to alter what is actually in $_POST, imho. And you also don't really want to add new elements to an array while you iterate through it, generally... Though I think maybe foreach does the right thing with that. Better to build a new array and put the stuff you want into that. -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Alter an Array Key
1. Don't modify $_POST 2. You controll the name of the array keys with the form. Why is there any need to change them form PHP's side of things? 3. I'm not sure Roberts solution would work. I think it might result in an endless loop, and timeout your script. - Craige -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php