Skip to content

How to Trim all Members of an array with PHP?

Last updated on January 17, 2018

This Post is About Removing White space of array Members from the beginning and end of the given array. Here is the code.

 arjun
    [1] => naveen
    [2] => Raj
    [3] => Mahesh
)
*/

Another Method :

// names array 
$names = array('arjun ',' naveen ','Raj    ','Mahesh  ');

//  trim array values using this function "trim_filter"
function trim_filter(&$names) {
	$names = trim($names);  
	// this removes whitespace from the beginning and end of the name
}

array_filter($names, 'trim_filter');  //the data in $names is trimmed
print_r($names);

Used PHP functions

array_map() — Applies the callback to the elements of the given array, returns result in new array

trim() — Strip white-space (or other characters) from the beginning and end of a string

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments