Home >Backend Development >PHP Problem >How to remove empty strings from array in php

How to remove empty strings from array in php

PHPz
PHPzOriginal
2023-04-27 16:24:161428browse

In PHP, an array is a very common data structure that can be used to store multiple values, and these values ​​can be of any type. In actual applications, we often encounter a situation: empty strings appear in the array, and we want to remove them from the array in order to better process other data. This article will introduce how to remove empty strings from an array using PHP.

PHP provides a variety of methods to remove empty strings from arrays. The following are some of the most popular methods:

Method 1: Use the array_filter function

The array_filter function is A very useful function in PHP, which can be used to filter arrays. This function returns a new array that contains only the elements in the original array that meet the specified conditions.

We can filter out empty strings in the array by specifying a callback function using the array_filter function. The callback function needs to receive each array element as a parameter and return a Boolean value. If this boolean value is true, then the element will be retained in the new array, otherwise, the element will be removed from the new array.

The following is an example of using the array_filter function to remove empty strings in an array:

// 示例数组
$arr = array("hello", "world", "", "php", "", "is", "cool");

// 使用 array_filter 过滤空字符串
$filtered_arr = array_filter($arr, function($value) {
    return $value !== "";
});

// 打印结果
print_r($filtered_arr);

In the above example, we created an array $arr containing empty strings. Using the array_filter function, we remove the empty strings from this array and store them in the $filtered_arr variable. Finally, use the print_r function to print the results.

Method 2: Use foreach to traverse the array and delete the empty string

Another way is to use a foreach loop to traverse the array and use the unset function to remove the empty string. Here is an example of using a foreach loop to remove empty strings from an array:

// 示例数组
$arr = array("hello", "world", "", "php", "", "is", "cool");

// 遍历数组并移除空字符串
foreach($arr as $key => $value) {
    if($value === "") {
        unset($arr[$key]);
    }
}

// 重新排序并打印结果
$arr = array_values($arr);
print_r($arr);

In the above example, we first create an array $arr containing empty strings. In the foreach loop, we use the if statement to determine whether the current element is an empty string. If so, we use the unset function to remove the element. Finally, we use the array_values ​​function to reorder the array and print the results.

Method 3: Using the array_walk function

Finally, we introduce a method of using the array_walk function. This function will iterate through each element in the array and call the user-defined callback function to process each element.

We can determine whether the current element is an empty string in the custom function, and if so, remove the element. The following is an example of using the array_walk function to remove empty strings in an array:

// 示例数组
$arr = array("hello", "world", "", "php", "", "is", "cool");

// 移除空字符串
function remove_empty_string(&$item, $key)
{
  if($item === "") {
     unset($item);
  }
}

// 使用 array_walk 移除空字符串
array_walk($arr, 'remove_empty_string');

// 重新排序并打印结果
$arr = array_values($arr);
print_r($arr);

In the above example, we first create an array $arr containing empty strings. Next, we define a callback function called remove_empty_string, which removes empty strings. Finally, we use the array_walk function to walk through the array and call the callback function for each element in the array. Finally, we use the array_values ​​function to reorder the array and print the results.

Summary

In this article, we introduced how to remove empty strings from arrays in PHP. We introduced three different methods: using the array_filter function, using a foreach loop to traverse the array and remove empty strings, and using the array_walk function to remove empty strings. No matter which method you choose, you can quickly and easily remove empty strings from an array, making your code cleaner and more readable.

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