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"; } } Link to comment https://forums.phpfreaks.com/topic/121451-solved-removing-empty-values-from-_post-array/ 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"; } } ?> Link to comment https://forums.phpfreaks.com/topic/121451-solved-removing-empty-values-from-_post-array/#findComment-626278 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. Link to comment https://forums.phpfreaks.com/topic/121451-solved-removing-empty-values-from-_post-array/#findComment-626298 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.