Home  >  Article  >  Backend Development  >  How to delete empty elements in an array in php

How to delete empty elements in an array in php

PHPz
PHPzOriginal
2023-04-26 09:16:57851browse

In PHP development, arrays are often used to store and manage data. However, sometimes the array contains some empty elements, which wastes memory and makes the code difficult to maintain and understand. Therefore, knowing how to remove empty elements from an array is a skill worth learning.

In PHP, there are many ways to delete empty elements in an array. Two of these methods will be introduced below.

Method 1: Use the array_filter() function

The array_filter() function is a built-in function in PHP, used to process arrays. It can filter elements in an array based on specified conditions and return a new filtered array.

Based on this feature, we can use the array_filter() function to delete empty elements in the array.

The following is the sample code:

<?php
// 原始数组
$arr = array(&#39;hello&#39;, &#39;&#39;, &#39;world&#39;, &#39;&#39;, &#39;&#39;, &#39;PHP&#39;);
print_r($arr);

// 过滤空元素
$arr = array_filter($arr);
print_r($arr);
?>

Output result:

Array
(
    [0] => hello
    [1] => 
    [2] => world
    [3] => 
    [4] => 
    [5] => PHP
)
Array
(
    [0] => hello
    [2] => world
    [5] => PHP
)

You can see that using the array_filter() function to obtain a new array, there are no empty spaces in the new array element.

Method 2: Use the unset() function

Another method is to use PHP’s built-in unset() function. The unset() function is used to delete a variable and can delete the specified element in the array. We can use the foreach loop statement to traverse the array to determine whether each element is empty. If it is empty, use the unset() function to delete it.

The following is the sample code:

<?php
// 原始数组
$arr = array(&#39;hello&#39;, &#39;&#39;, &#39;world&#39;, &#39;&#39;, &#39;&#39;, &#39;PHP&#39;);
print_r($arr);

// 删除空元素
foreach ($arr as $key => $value) {
    if ($value === '') {
        unset($arr[$key]);
    }
}
print_r($arr);
?>

Output result:

Array
(
    [0] => hello
    [1] => 
    [2] => world
    [3] => 
    [4] => 
    [5] => PHP
)
Array
(
    [0] => hello
    [2] => world
    [5] => PHP
)

As you can see, using the foreach loop statement and the unset() function, we also get an empty element new array.

Comparison of the two methods

The method of using the array_filter() function is more concise and clearer than the method of using the foreach loop statement and the unset() function. The array_filter() function has its own filtering function, which can solve the problem with one line of code. The foreach loop statement needs to use an if statement to determine whether each element is empty before deleting it. The code does not look so concise.

On the other hand, when dealing with large arrays, the method of using the unset() function may be more efficient. Because when using the array_filter() function, a new array needs to be returned, which consumes additional memory and time.

Summary

Deleting empty elements in an array is a common requirement in PHP development. Knowing how to remove empty elements can improve code quality and efficiency. Both the array_filter() function and the unset() function can implement this function. Just choose the appropriate method according to your needs.

The above is the detailed content of How to delete empty elements in an 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