This article describes how to clear spaces at both ends of all strings in the PHP array. It mainly involves operations on PHP arrays and string operations. It is very useful. For more information, see
This article describes how to clear spaces at both ends of all strings in the PHP array. It mainly involves operations on PHP arrays and string operations. It is very useful. For more information, see
This article describes how to clear spaces at both ends of all strings in the PHP array. The specific implementation method is as follows:
In general, there are many implementation methods to clear spaces in strings in php, but we cannot simply use these methods to clear the front and back codes of all values in the array ,, in this example, the array_map function exclusive to php is used to traverse and clear spaces at both ends of all strings in the array.
The specific implementation code is as follows:
The Code is as follows:
Function TrimArray ($ Input ){
If (! Is_array ($ Input ))
Return trim ($ Input );
Return array_map ('trimarray', $ Input );
}
/*
Old version (v0.1): the Old version is for your reference:
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;
}
*/
// Demo:
$ 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"
}
}
I hope this article will help you with PHP programming.