Home  >  Article  >  Backend Development  >  How to clear spaces in array in php

How to clear spaces in array in php

青灯夜游
青灯夜游Original
2021-03-26 11:29:502850browse

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)}".

How to clear spaces in array in php

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.

How to clear spaces in array in php

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.

How to clear spaces in array in php

In the test.php file, assign the array with spaces to the $arr variable.

How to clear spaces in array in php

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.

How to clear spaces in array in php

In the test.php file, use echo to output the array with spaces removed.

How to clear spaces in array in php

Open the test.php file in the browser to view the results.

How to clear spaces in array in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Two unknown PHP operatorsNext article:Two unknown PHP operators