PHP is a popular open source programming language that is widely used in web development. In PHP development, we often need to handle various operations on arrays. When we operate arrays, we sometimes encounter some problems, such as how to remove empty arrays.
In PHP, how to determine whether an array is empty? We can use the empty() function or count() function to determine whether an array is empty. The empty() function can determine whether the array is empty or has no elements. If it is an empty array, it returns true, otherwise it returns false. The count() function can count the number of elements in the array. If the number of elements is 0, it means that the array is empty.
So how to remove the empty array? When processing arrays, we can use various methods, such as looping through the array, filtering the array, and so on. Let's introduce several methods to remove empty arrays.
Method 1. Use the array_filter() function
The array_filter() function is a function provided by PHP for filtering arrays. It can delete elements in the array that do not meet the specified conditions. We can remove empty arrays by using the array_filter() function. The code is as follows:
$array = array('', 'a', 'b', 0, NULL, 'c', array()); $array = array_filter($array,'strlen'); print_r($array);
In the above code, we first define an array containing an empty array. Then by using the array_filter() function, a callback function strlen is passed, which can count the length of the string. In this example, we use the strlen function as a callback function that can delete empty strings and empty arrays, and finally get an array that does not contain empty arrays.
Method 2. Use foreach loop to traverse the array
We can also use foreach loop to traverse the array to remove empty arrays. The code is as follows:
$array = array('', 'a', 'b', 0, NULL, 'c', array()); foreach ($array as $key => $value) { if (empty($value)) { unset($array[$key]); } } print_r($array);
In the above code, we use a foreach loop to traverse the array. If the element is empty, use the unset() function to delete the element. You end up with an array that does not contain an empty array.
Method 3. Use the array_values() function
The array_values() function can return all the values in the array as a new array. This function can be applied to the array after removing the empty array in method one and method two, and finally returns a new array. The code is as follows:
$array = array('', 'a', 'b', 0, NULL, 'c', array()); $array = array_values(array_filter($array,'strlen')); print_r($array);
or
$array = array_values($array); foreach ($array as $key => $value) { if (empty($value)) { unset($array[$key]); } } $array = array_values($array); print_r($array);
In the above code, we use the array_values() function to rearrange all elements in the filtered array and return a new array.
Summary
The above describes how to use PHP to remove empty arrays, including using the array_filter() function, foreach loop to traverse the array, and using the array_values() function. But in practical applications, we also need to choose an appropriate method according to the specific situation.
The above is the detailed content of How to remove empty array in php. For more information, please follow other related articles on the PHP Chinese website!