/** * 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
时间: 2024-10-18 02:26:26