PHP clears spaces at both ends of all strings in the array. PHP clears spaces at both ends of all strings in the array. This article describes how PHP clears spaces at both ends of all strings in the array. PHP clears spaces at both ends of all strings in the array.
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 code before and after 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.
How can I use a php program to delete "spaces" in "string element" in "array "?
$ Arr = array ();
$ Arr [] = "ad dfd ";
$ Arr [] = "saf sdf dsf ";
$ Arr [] = "sdf dsfgd dd ";
$ Arr [] = "dfd dfferw ";
While (list ($ name, $ value) = each ($ arr )){
Echo $ value;
$ Arr2 [] = trim ($ value); // Remove space
}
Print_r ($ arr2); // This should be the array you want ~
?>
How to remove unnecessary spaces in the string array
In fact, this algorithm only shifts each space backward. The problem is that it crashes when it encounters two spaces...
You can change it a bit. when a space is encountered, it is not exchanged with the next character. Instead, it is used to retrieve the next non-space character and exchange it. In the end, it is OK to remove the extra space.
Examples in this article describes how to clear spaces at both ends of all strings in the PHP array ....