How to clear spaces in array in php

青灯夜游
Release: 2023-03-09 06:58:01
Original
2834 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!