Friday, October 10, 2025
HomeLanguagesHow to trim all strings in an array in PHP ?

How to trim all strings in an array in PHP ?

Given a string array with white spaces, the task is to remove all white spaces from every object of an array.
Examples: 
 

Input: arr = ["Geeks  ", "   for", "  Geeks  "]
Output:
Geeks
for
Geeks

Method 1: Using trim() and array_walk() function: 
 

  • trim() Function: The trim() function is an inbuilt function which removes whitespaces and also the predefined characters from both sides of a string that is left and right.
    Syntax: 
     
trim( $string, $charlist )
  • array_walk() Function: The array_walk() function is an inbuilt function in PHP which walks through the entire array regardless of pointer position and applies a callback function or user-defined function to every element of the array. The array element’s keys and values are parameters in the callback function.
    Syntax: 
     
boolean array_walk( $array, myFunction, $extraParam )

Example: 
 

php




<?php
 
// Create an array with whitespace
$arr = array( "Geeks  ", "  for", "   Geeks   ");
 
// Print original array
echo "Original Array:\n";
foreach($arr as $key => $value)
    print($arr[$key] . "\n");
 
// Iterate array through array_walk() function
// and use trim() function to remove all
// whitespaces from every array objects
array_walk($arr, create_function('&$val',
                    '$val = trim($val);'));
 
// Print modify array
echo "\nModified Array:\n";
foreach($arr as $key => $value)
    print($arr[$key] . "\n");
     
?>


Output: 

Original Array:
Geeks  
  for
   Geeks   

Modified Array:
Geeks
for
Geeks

 

Method 2: Using trim() and array_map() function: 
 

  • The array_map() function is an inbuilt function in PHP which helps to modify all elements one or more arrays according to some user-defined condition in an easy manner. It basically, sends each of the elements of an array to a user-defined function and returns an array with new values as modified by that function.
    Syntax: 
     
array_map(functionName, arr1, arr2...)

Example: 
 

php




<?php
 
// Create an array with whitespace
$arr = array( "Geeks  ", "  for", "   Geeks   ");
 
// Print original array
echo "Original Array:\n";
foreach($arr as $key => $value)
    print($arr[$key] . "\n");
 
// Iterate array through array_map() function
// Using trim() function to remove all
// whitespaces from every array objects
$arr = array_map('trim', $arr);
  
// Print modify array
echo "\nModified Array:\n";
foreach($arr as $key => $value)
    print($arr[$key] . "\n");
     
?>


Output: 

Original Array:
Geeks  
  for
   Geeks   

Modified Array:
Geeks
for
Geeks

 

Method 3: Basic method using for loop: Use for loop to traverse each element of array and then use trim() function to remove all white space from array elements.
Example: 
 

php




<?php
 
// Create an array with whitespace
$arr = array( "Geeks  ", "  for", "   Geeks   ");
 
// Print original array
echo "Original Array:\n";
foreach($arr as $key => $value)
    print($arr[$key] . "\n");
  
// Print modify array
echo "\nModified Array:\n";
 
// Iterate array through for loop
// Using trim() function to remove all
// whitespaces from every array objects
foreach($arr as $key => $value) {
    $arr[$key] = trim($value);
      
    // Print current object of array
    print($arr[$key] . "\n");
}
     
?>


Output: 

Original Array:
Geeks  
  for
   Geeks   

Modified Array:
Geeks
for
Geeks

 

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS