使用php独有的array_map函数遍历清除数组中所有字符串的两端空格
/** * 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" } } */