tiny class of tools to make it happen.
class Tools {
public static function recursiveTrim($input) {
$return_object = false;
if (is_object($input)) {
$return_object = true;
$input = self::object2array($input);
}
array_walk_recursive($input, 'trim');
if ($return_object && is_array($input)) {
$input = self::array2object($input);
}
return $input;
}
public static function array2object($array) {
$obj = new stdClass;
foreach ($array as $k => $v) {
if (strlen($k)) {
if (is_array($v)) {
$obj->{$k} = self::array2object($v); //RECURSION
} else {
$obj->{$k} = $v;
}
}
}
return $obj;
}
public static function object2array($obj) {
$arrObj = is_object($obj) ? get_object_vars($obj) : $obj;
foreach ($arrObj as $key => $val) {
$val = (is_array($val) || is_object($val)) ? self::object2array($val) : $val;
$arr[$key] = $val;
}
return $arr;
}
}
No comments:
Post a Comment
You can ask IT questions in comments!