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-19 14:15:081243browse

PHP is a popular server-side programming language that is widely used for web development. In PHP, an array is a very useful data structure that can store multiple values. However, in actual development, we often need to delete empty strings from arrays. This article will introduce how to use PHP to remove empty strings from an array.

1. Use the array_filter function

In PHP, there is a function called array_filter, which can filter elements in the array that meet certain conditions. We can use this function to delete empty strings in the array. The specific implementation is as follows:

$array = array('a', '', 'b', '', 'c');
$array = array_filter($array, 'strlen');

In the above code, $array is the array to be processed. We use the array_filter function and the strlen function to delete empty strings. The strlen function returns the string length, so if the length of the element is 0, it is considered an empty string and will be deleted. The end result is that $array contains only non-empty strings.

2. Use foreach loop

If you don’t want to use the array_filter function, you can also use foreach loop to delete empty strings in the array. The implementation method is as follows:

$array = array('a', '', 'b', '', 'c');
foreach ($array as $key => $value) {
    if ($value == '') {
        unset($array[$key]);
    }
}

In the above code, we use a foreach loop to traverse the array. For each element, if its value is equal to the empty string, use the unset function to delete it from the array. The end result is that $array contains only non-empty strings.

3. Use the array_diff function

Another useful function in PHP is array_diff, which can calculate the difference of multiple arrays. We can remove the empty strings by taking the difference between the original array and an array containing only empty strings. The specific implementation is as follows:

$array = array('a', '', 'b', '', 'c');
$empty_string_array = array('');
$array = array_diff($array, $empty_string_array);

In the above code, we use the array_diff function to find the difference between $array and $empty_string_array, so the final result is that $array only contains non-empty strings.

In summary, we have introduced three methods to use PHP to remove empty strings from arrays. The array_filter function is the simplest method, the foreach loop is more flexible, and the array_diff function can handle multiple arrays. Whichever method you choose, you will efficiently handle empty strings in the array, making the code cleaner and easier to understand.

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