PHP Function: array_trim

I was loading alot of data from .csv files, some of the data was blank and i didnt want array key=>values for it, so i created an multi dimensional array cleansing tool.

function array_trim(&$a){
	foreach( $a as $k => $v )
	{	
		if ( is_array( $a[$k] ) ) array_trim( $a[$k] );
		else $a[$k] = trim( $v );
		if ( empty($a[$k]) ) unset( $a[$k] );
	}
} 
// Useage:
array_trim( $a );

Have fun.

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

Nice function, thanks for sharing.
Typically l use array_walk() for a similar result.

Example:
function trim_value(&$value) {
$value = trim($value);
}
$arr = array(‘demo1 ‘, ‘demo2′, ‘ demo 3 ‘);
print_r(array_walk($arr));

Leave a comment

(required)

(required)