I just can't figure out how to do this, I was trying to do this but didn't work 🙁

function remove($myarray){
	if (in_array(" ", $myarray)){
		foreach($myarray as $show){
			if(($show == " ") || (empty($show))){
				unset($show);
				exit();
			}
		}
		exit();
	}
	return $myarray;
}

Any ideas ?

    You're unsetting the temporary variable name, not the array. I'm a little tired, but what you want to do is get the key, then unset the actual array key that is empty. Something like this

    function remove($myarray){
        if (in_array(" ", $myarray)){
            foreach($myarray as $key => $value){
                if(($value == " ") || (empty($value))){
                    unset($myarray["$key"]);
                }
            }
        }
        return $myarray;
    }

    Cgraz

      It worked but what if $myarray comes from

      trim($_POST['name']);
      $myarray = explode(" ", $_POST['name']);
      

      What I want to do is remove all the spaces from the $_POST

      so if in the input form someone writes "John (5 spaces) Smith "

      transform this into "John Smith" my idea was to explode this, then remove empty values in the array and then implode the new array with just one space :p

        Try:

        $s = "John     Smith";
        echo preg_replace("/\s+/","\040",$s);
        

        Edit: Had to use the code tags, what is up with the php tags?

          thanks a lot 😃

          I didn't used preg_replace before can you please explain me a bit what means

          040

          I know regex from Perl, but I don't know the meaning of those numers, or can you give me a reference to read about it ?

          Thanks 🙂

            Write a Reply...