How can I trim all strings in an Array?

PhpArraysTrim

Php Problem Overview


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

Php Solutions


Solution 1 - Php

array_map() is what you need:

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

Solution 2 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanielView Question on Stackoverflow
Solution 1 - PhpzerkmsView Answer on Stackoverflow
Solution 2 - PhpKingCrunchView Answer on Stackoverflow