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

How to delete empty array in php

PHPz
PHPzOriginal
2023-04-25 18:22:13979browse

In PHP language, deleting empty arrays is a common operation. When we use arrays as data structures, sometimes we may need to delete empty arrays in them for better use and analysis of the data.

In the following, we will introduce how to delete empty arrays using PHP language.

Method 1: Use foreach loop

Use foreach loop to traverse the array, find the keys of all empty arrays, and then use the unset function to delete them. The code is as follows:

$array = array(1, 2, array(), 4, array());
foreach ($array as $key => $value) {
    if (empty($value)) {
        unset($array[$key]);
    }
}

In this example, we define an array containing integers and an empty array. Then we use a foreach loop to iterate through the array. If the current value in the array is empty, it is removed along with the key using the unset function.

Note: This method may cause index reordering, so it is not recommended to use this method to delete non-empty arrays.

Method 2: Use the array_filter function

Use the array_filter function, which will return a new array containing all elements that are not empty in the source array. The code is as follows:

$array = array(1, 2, array(), 4, array());
$array = array_filter($array, function($value) {
    return !empty($value);
});

In this example, we define an array containing integers and an empty array. We then use the array_filter function to filter out all empty arrays and store the results back into the original array.

This method is recommended because the keys and values ​​of the original array can be retained and will not cause rearrangement.

Method 3: Use the array_diff function

Use the array_diff function, which can compare two or more arrays and return a new array that contains everything in the first array but not in other arrays value. The code is as follows:

$array = array(1, 2, array(), 4, array());
$array = array_diff($array, array(array()));

In this example, we define an array containing integers and an empty array. We then use the array_diff function to compare all empty arrays with an array containing empty arrays and return a new array that does not contain empty arrays.

This method is also recommended because it preserves the keys and values ​​of the original array and does not cause rearrangement.

Summary

The above method provides a variety of methods to delete empty arrays, and you can choose one of them according to your own needs. Regardless of which method is used, care should be taken not to use unsafe functions to handle arrays to ensure the safety and reliability of the code.

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