How to trim white spaces of array values in php

后端 未结 12 1425
旧巷少年郎
旧巷少年郎 2020-11-28 19:30

I have an array as follows

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

I want an array which conta

12条回答
  •  不知归路
    2020-11-28 20:05

    If the array is multidimensional, this will work great:

    //trims empty spaces in array elements (recursively trim multidimesional arrays)
    function trimData($data){
       if($data == null)
           return null;
    
       if(is_array($data)){
           return array_map('trimData', $data);
       }else return trim($data);
    }
    

    one sample test is like this:

    $arr=[" aaa ", " b  ", "m    ", ["  .e  ", "    12 3", "9 0    0 0   "]];
    print_r(trimData($arr));
    //RESULT
    //Array ( [0] => aaa [1] => b [2] => m [3] => Array ( [0] => .e [1] => 12 3 [2] => 9 0 0 0 ) )
    

提交回复
热议问题