Trim array values in php

The trim() function of php removes whitespaces or some other character from a string like...

$trimmed = trim ($string);

In above example trim removes only whitespaces but you can remove any character by passing that character set into the function as second parameter like...

$trimmed = trim ($string, '/-');

This removes / and - from the string from left and right.

But when it comes to array what to do?

Just use the same trim() function with combination of another php function array_map() like...

     $trimmed = array_map('trim', $array);

Above example will trim all array values.

array_map() is a very handy function while dealing with arrays.
Look at the another example of array_map() to Sort multidimensional array.

Trim array values in php Trim array values in php Reviewed by Altaf Hussain on September 27, 2013 Rating: 5

No comments:

Altaf Web. Powered by Blogger.