Change language

How to trim all strings in an array in PHP?

Examples :
Input: arr = ["Geeks", "for", "Geeks"] Output: Geeks for Geeks 
Method 1. Using the functions trim() and array_walk() :
  • Functiontrim(): trim() function - it is a built-in function that removes spaces as well as predefined characters from both sides of the string to the left and right. Syntax:
    trim ($string, $charlist) 
  • array_walk() function:array_walk() function - it is a built-in function in PHP that walks through the entire array regardless of pointer position and applies a callback or custom function to each element in the array. The keys and values ​​of the array element are parameters in the callback function. Syntax:
    boolean array_walk ($array, myFunction, $extraParam) 
Example :  
// Create an array with spaces $arr = array ( " Geeks " , " for " , " Geeks " );  
// Print the original array echo "Original Array:" ; foreach ( $arr as $key = > $value ) print ( $arr [ $key ]. "" );  
// Iterate the array through the array_walk() function
// and use the trim() function to remove all
// spaces from each object in the array array_walk ( $arr , create_function ( ’& amp; $val’ , ’$val = trim ($val);’ ));  
// Print modify array echo "Modified Array:" ; foreach ( $arr as $key = > $value ) print ( $arr [ $key ]. "" );  
?>
Exit:
Original Array: Geeks for Geeks Modified Array: Geeks for Geeks 
Method 2. Using functions trim() and array_map() :
  • array_map() function - it is a built-in function in PHP that helps to easily modify all elements of one or more arrays according to some custom conditions. Basically, it sends each of the array elements to a custom function and returns an array with the new values ​​modified by that function. Syntax:
    array_map (functionName, arr1, arr2. ..) 
Example :  
// Create an array with spaces $arr = array ( "Geeks " , " for" , " Geeks " );  
// Print the original array echo "Original Array:" ; foreach ( $arr as $key = > $value ) print ( $arr [ $key ]. "" );  
// Iterate the array through array_map()
// Using trim() to remove all
// spaces from each object in the array $arr = array_map ( ’ trim’ , $arr );  
// Print modify array echo "Modified Array:" ; foreach ( $arr as $key = > $value ) print ( $arr [ $key ]. "" );  
?>
Exit:
Original Array: Geeks for Geeks Modified Array: Geeks for Geeks 
Method 3: Basic method using a for loop:Use a for loop to traverse each element of the array, and then use the trim() function to remove all white space from array elements.Example :  
// Create an array with spaces $arr = array ( "Geeks " , " for" , " Geeks " );  
// Print the original array echo "Original Array:" ; foreach ( $arr as $key = > $value ) print ( $arr [ $key ]. "" );  
// Print modify array echo "Modified Array:" ;  
// Loop through the array for a for loop // Using trim() to remove all
// spaces from each object in the array foreach ( $arr as $key = > $value ) { $arr [ $key ] = trim ( $value ); // Print the current array object print ( $arr [ $key ]. "" ); }  
?>
Exit:
Original Array: Geeks for Geeks Modified Array: Geeks for Geeks 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method