array_walk question

Hi Guys!

I am trying to use array_walk like so, but it’s giving an error:


if(!empty($_POST['language'])){
				$languages = explode(",", $_POST['language']);
				$languages = array_walk($languages, 'trim');
				$languages = array_walk($languages, 'strtolower');
				$languages = array_walk($languages, 'ucfirst');
				$languages = serialize($languages);
			}

Am I using it in the correct ayntax?

array_walk is not the correct function to use here since that works on function with two parameters

The functions strtolower and ucfirst only expect one parameter, so that’s not going to work.
Also array_walk doesn’t return the modified array, but a boolean to indicate if the mapping was succesful. So if you were to use array_walk (which you shouldn’t), it should be:


if(!empty($_POST['language'])){
  $languages = explode(",", $_POST['language']);
  array_walk($languages, 'trim');
  array_walk($languages, 'strtolower');
  array_walk($languages, 'ucfirst');
  $languages = serialize($languages);
}

Now what you should use is [fphp]array_map[/fphp]:


if(!empty($_POST['language'])){
  $languages = explode(",", $_POST['language']);
  $languages=array_map($languages, 'trim');
  $languages=array_map($languages, 'strtolower');
  $languages=array_map($languages, 'ucfirst');
  $languages = serialize($languages);
}

Or, to make it freaky:


if(!empty($_POST['language'])){
  $languages = serialize(
    array_map('ucfirst', 
       array_map('strtolower', 
         array_map('trim', 
           explode(",", $_POST['language'])
         )
       )
    )
  );
}

:slight_smile: