184

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
  • 26,397
  • 17
  • 98
  • 144
Daniel
  • 2,311
  • 4
  • 16
  • 12

2 Answers2

468

array_map() is what you need:

$result = array_map('trim', $source_array);
zerkms
  • 240,587
  • 65
  • 429
  • 525
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
  • 124,572
  • 19
  • 146
  • 171