chaseman Posted May 9, 2011 Share Posted May 9, 2011 Is there a way to apply the function trim() on a whole array, instead of applying it to the individual input fields one by one? Here's the code: $blurb1_title = array ( $_POST['blurb1_title1'], $_POST['blurb1_title2'], $_POST['blurb1_title3'], $_POST['blurb1_title4'], $_POST['blurb1_title5'] ); $blurb2_title = array ( $_POST['blurb2_title1'], $_POST['blurb2_title2'], $_POST['blurb2_title3'], $_POST['blurb2_title4'], $_POST['blurb2_title5'] ); $blurb3_title = array ( $_POST['blurb3_title1'], $_POST['blurb3_title2'], $_POST['blurb3_title3'], $_POST['blurb3_title4'], $_POST['blurb3_title5'] ); All those $_POST variables are input fields, and I would like to apply trim() to them so the spaces get removed. Is there an efficient way to do it without having to apply it all individually, if yes, what would be the correct syntax? Quote Link to comment https://forums.phpfreaks.com/topic/235940-apply-trim-on-whole-array/ Share on other sites More sharing options...
fugix Posted May 9, 2011 Share Posted May 9, 2011 yeah you could use a foreach() loop to pull out each key and trim each one...not sure if thats what you're looking for Quote Link to comment https://forums.phpfreaks.com/topic/235940-apply-trim-on-whole-array/#findComment-1212869 Share on other sites More sharing options...
PFMaBiSmAd Posted May 9, 2011 Share Posted May 9, 2011 See this post - http://www.phpfreaks.com/forums/index.php?topic=332561.msg1565865#msg1565865 Quote Link to comment https://forums.phpfreaks.com/topic/235940-apply-trim-on-whole-array/#findComment-1212873 Share on other sites More sharing options...
chaseman Posted May 9, 2011 Author Share Posted May 9, 2011 Oops I accidentally posted my reply to the other thread. Well here's the reply again. Thanks PFM, I tried to implement it but I was not able to do so, I tried the following: function blurb_titles ($all_titles) { trim($all_titles); } array_walk ($blurb2_title, 'blurb_titles'); Which I added below the array list. But this would only break my script. Quote Link to comment https://forums.phpfreaks.com/topic/235940-apply-trim-on-whole-array/#findComment-1212884 Share on other sites More sharing options...
AbraCadaver Posted May 9, 2011 Share Posted May 9, 2011 First, he said to use array_map() not array_walk() and you need to run the array through trim: $blurb2_title = array_map('trim', $blurb2_title); Quote Link to comment https://forums.phpfreaks.com/topic/235940-apply-trim-on-whole-array/#findComment-1212891 Share on other sites More sharing options...
chaseman Posted May 9, 2011 Author Share Posted May 9, 2011 What I tried was array_map (trim(), $blurb2_title); Didn't think that your version will work too, but now that I look at it it seems obvious. Trim is no different than any other function, it's just global. Thanks abra! Quote Link to comment https://forums.phpfreaks.com/topic/235940-apply-trim-on-whole-array/#findComment-1212896 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.