Home > Article > Backend Development > How to clear spaces in array in php
Clearing method: First use a foreach loop to traverse the array; then use the str_replace() function to replace the spaces in each array element with nothing. The syntax is "foreach($arr as $k=$v){$ arr[$k]=str_replace(' ','',$v)}".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php clears spaces in the array Method:
Create a new php file and name it test.php for explanation.
In the test.php file, use the header() method to set the encoding format of the page to utf-8 to avoid garbled characters when the page outputs Chinese.
In the test.php file, assign the array with spaces to the $arr variable.
In the test.php file, use a foreach loop to traverse the array, and then use the str_replace function to replace the spaces in each array element with empty spaces to delete spaces.
In the test.php file, use echo to output the array with spaces removed.
Open the test.php file in the browser to view the results.
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to clear spaces in array in php. For more information, please follow other related articles on the PHP Chinese website!