How can I trim all strings in an Array?

Daniel picture Daniel · Jul 21, 2011 · Viewed 96.4k times · Source

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");

Answer

zerkms picture zerkms · Jul 21, 2011

array_map() is what you need:

$result = array_map('trim', $source_array);