Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I trim all strings in an Array? [duplicate]

Tags:

arrays

php

trim

If I have this array:

array("  hey  ", "bla  ", "  test"); 

and I want to trim all of them, How can I do that?

The array after the trim:

array("hey", "bla", "test"); 
like image 630
Daniel Avatar asked Jul 20 '11 22:07

Daniel


People also ask

Does trim modify the original string?

trim() The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string.


2 Answers

array_map() is what you need:

$result = array_map('trim', $source_array); 
like image 185
zerkms Avatar answered Oct 23 '22 19:10

zerkms


array_map() applies a given callback to every value of an array and return the results as a new array.

$array = array_map('trim', $array); 
like image 30
KingCrunch Avatar answered Oct 23 '22 19:10

KingCrunch