196

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");
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
Daniel
  • 2,441
  • 4
  • 18
  • 12

2 Answers2

489

array_map() is what you need:

$result = array_map('trim', $source_array);
zerkms
  • 249,484
  • 69
  • 436
  • 539
70

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);
KingCrunch
  • 128,817
  • 21
  • 151
  • 173