How to trim white spaces of array values in php

I have an array as follows

$fruit = array('  apple ','banana   ', ' , ',     '            cranberry ');

I want an array which contains the values without the white spaces on either sides but it can contain empty values how to do this in php.the output array should be like this

$fruit = array('apple','banana', ',', 'cranberry');

Answer

array_map and trim can do the job

$trimmed_array = array_map('trim', $fruit);
print_r($trimmed_array);

Leave a Reply

Your email address will not be published. Required fields are marked *