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

How to remove spaces in array in php

PHPz
PHPzOriginal
2023-04-20 10:11:31915browse

In PHP, array is a very important data type, which can be used to store a set of data. When we need to process this data, sometimes we encounter unnecessary spaces in the array, and these spaces need to be removed. This article will introduce how to remove spaces from arrays in PHP.

1. Use the trim() function to remove spaces from array elements

PHP’s built-in function trim() can remove spaces at both ends of a string, and it also applies to array elements. We can loop through the array and perform the trim() operation on each element in the array. The specific example is as follows:

$array = array("first ", "second", " third ");
foreach($array as &$value){
    $value = trim($value);
}
unset($value); //销毁指针
print_r($array); //输出结果:Array([0] => first [1] => second [2] => third)

First, we create an array $array and initialize it to an array containing three elements with unnecessary spaces in the elements. Next, we use a foreach loop to iterate through the array and use the & symbol to get a reference to the array element. In the loop body, the trim() function is called to remove spaces at both ends of the string, and finally the unset() function is used to destroy the pointer to the last element of the array.

2. Use the array_map() function to remove spaces from array elements

In addition to using a loop to traverse the array, we can also use the array_map() function to remove spaces from array elements. The array_map() function can receive a callback function as a parameter. This callback function will operate on each element in the array and store the result in a new array. The specific example is as follows:

$array = array("first ", "second", " third ");
$array = array_map('trim',$array); //使用 trim() 函数对每个元素进行操作
print_r($array); //输出结果:Array([0] => first [1] => second [2] => third)

We first created an array $array with three elements, and the elements contained unnecessary spaces. Then, use the array_map() function to perform the trim() operation on each element, and store the result in a new array. Finally, use the print_r() function to output the array, and you can see that the spaces have been removed.

3. Use the preg_replace() function to remove spaces from array elements

In addition to using PHP built-in functions, we can also use regular expressions to remove spaces from array elements. The specific implementation plan is as follows:

$array = array("first ", "second", " third ");
foreach($array as &$value){
    $value = preg_replace('/\s+/','',$value);
}
unset($value); //销毁指针
print_r($array); //输出结果:Array([0] => first [1] => second [2] => third)

In this implementation plan, we first use the foreach loop to traverse the array, and use the & symbol to obtain the reference of the array element. Within the body of the loop, use the preg_replace() function to find spaces in the elements and replace them with empty strings. Finally, use the unset() function to destroy the pointer to the last element of the array.

4. Summary

To remove spaces from array elements in PHP, we can use trim() function, array_map() function, preg_replace() function and other means. In practical applications, we choose the most appropriate method according to our actual situation.

The above is the detailed content of How to remove 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