mdrisser Posted December 7, 2006 Share Posted December 7, 2006 first a bit of context, I have a form that contains checkboxes that are declared like this:[code]<input type="checkbox" name="zones[]" value="blah"/>[/code]After the form is submitted I put the checkbox values into a variable like so:[code]$i = $_POST['zones'];while(list($key,$value)=each($i)) { if(preg_match('/^(Array)/',$value)) { $value = preg_replace('/^(Array)/','//',$value); echo("Replaced Array<br/>"); } $zones .= $value . ",";}[/code]I'm trying to remove "Array" because, for whatever reason, the first element of the $i array always contains it.i.e. [code]$i[0] == "ArrayOceanside"[/code]I placed the echo() in for debugging, but it never gets to it. So I assume that for whatever reason my preg_match() is returning flase, but I just can't quite figure out why. Any help is greatly appreciated, as I'm running out of hair to pull out. ;) Quote Link to comment Share on other sites More sharing options...
c4onastick Posted December 7, 2006 Share Posted December 7, 2006 I'm not entirely sure why you have this:[code]while(list($key,$value)=each($i))[/code]I'd use:[code]foreach($i as $row)[/code]The keys for $_POST['zones'] are just 0, 1, 2...etc.I think the 'list' and 'each' in the 'while' (try reading that to someone that doesn't program!) are getting tripped up on each other. Your regex looks good, you don't need the parenthesizes around [code]'/^(Array)/'[/code]And the forward slashes in the replacement value shouldn't be there, unless you want to replace "Array" with "//"Try this:[code]$i = $_POST['zones'];foreach($i as $row) { if(preg_match('/^Array/',$row)) { $row = preg_replace('/^Array/', '' , $row); echo("Replaced Array<br/>"); } else { echo("Didn't Replace Array<br />"); }}[/code]Really, I don't think you'll have that problem if you use "foreach" instead of the "while" thing. Check the output of "foreach" before you code in the regex. Quote Link to comment Share on other sites More sharing options...
mdrisser Posted December 7, 2006 Author Share Posted December 7, 2006 c4,Thank you very, very much.The foreach() loop actually worked out better than I had expected. Running the foreach() negated the need for the regex at all.The code now reads:[code]if($_POST['zones'] != "") { $zones = $_POST['zones'];}foreach($zones as $value) { echo($value . "\n");}[/code]Which now outputs the array properly, i.e. without 'Array' being prepended to the first element of the array. ([i]I'm really not sure why it was happening to begin with, but I'll figure that out later I suppose, when I have more time.[/i]) You've just gotta love how changing one relatively small thing, can fix many problems Now its time to go get some hair plugs, so I have something to pull out next time....... ;D Quote Link to comment Share on other sites More sharing options...
c4onastick Posted December 7, 2006 Share Posted December 7, 2006 Glad to help. [quote author=mdrisser link=topic=117660.msg480615#msg480615 date=1165512499]Now its time to go get some hair plugs, so I have something to pull out next time....... ;D[/quote]Yikes! Good luck with that! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.