清除数组所有字符串元素两边的空格

/**
 * Trims a entire array recursivly.
 * 
 * @author      Jonas John
 * @version     0.2
 * @link        http://www.jonasjohn.de/snippets/php/trim-array.htm
 * @param       array      $Input      Input array
 */
function TrimArray($Input){
 
    if (!is_array($Input))
        return trim($Input);
 
    return array_map('TrimArray', $Input);
}
 
 
/* 
 
Old version (v0.1): 
 
function TrimArray($arr){
    if (!is_array($arr)){ return $arr; }
 
    while (list($key, $value) = each($arr)){
        if (is_array($value)){
            $arr[$key] = TrimArray($value);
        }
        else {
            $arr[$key] = trim($value);
        }
    }
    return $arr;
}
*/


//演示范例:
$DirtyArray = array(
    'Key1' => ' Value 1 ',
    'Key2' => '      Value 2      ',
    'Key3' => array(
        '   Child Array Item 1 ', 
        '   Child Array Item 2'
    )
);
 
$CleanArray = TrimArray($DirtyArray);
 
var_dump($CleanArray);
 
/*
Result will be:
 
array(3) {
  ["Key1"]=>
  string(7) "Value 1"
  ["Key2"]=>
  string(7) "Value 2"
  ["Key3"]=>
  array(2) {
    [0]=>
    string(18) "Child Array Item 1"
    [1]=>
    string(18) "Child Array Item 2"
  }
}
 
*/
//该代码片段来自于: http://www.sharejs.com/codes/php/2103
posted @ 2016-08-19 08:16  huerge  阅读(2610)  评论(0编辑  收藏  举报