kalevra Posted August 26, 2008 Share Posted August 26, 2008 I have a form submitted to a script. I get the values from the form using: foreach($_POST as $key=>$value){ $$key = $value; $search[] .= "$$key"; $replace[] .= "$value"; } (Variables $search & $replace are for use in a str_replace()) Now let's say some fields were left empty in the form I will have some $replace left empty. If I wanted to remove from both arrays the fields left empty, would this work? (Sorry I can't try myself no access to PHP server for next 2 hours:() Does it even set a $_POST variable if it's left empty? foreach($_POST as $key=>$value){ if(empty($value){ unset($_POST[$key]); }else{ $$key = $value; $search[] .= "$$key"; $replace[] .= "$value"; } } Quote Link to comment Share on other sites More sharing options...
zq29 Posted August 26, 2008 Share Posted August 26, 2008 You could check the contents while you're looping through the array... <?php foreach($_POST as $key=>$value){ if(!empty($value)) { $$key = $value; $search[] .= "$$key"; $replace[] .= "$value"; } } ?> Quote Link to comment Share on other sites More sharing options...
kalevra Posted August 26, 2008 Author Share Posted August 26, 2008 Doh! Even easier that way, Thanks a lot. 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.