php remove non-empty elements from array

王林
Release: 2023-05-29 09:16:37
Original
697 people have browsed it

In PHP development, arrays often need to be processed. One of the common questions is how to remove non-empty elements from an array. In this article, we will explore how to achieve this task using PHP.

First, let's take a look at the common array types in PHP. PHP supports two types of arrays: indexed arrays and associative arrays. Indexed arrays are arrays based on numeric indexes, while associative arrays are arrays based on string keys. Below is an example array:

$arr = array("apple", "", "banana", "orange", null, "peach");
Copy after login

In the above array, we have multiple non-null and empty elements. Our task is to remove these non-empty elements to obtain a completely empty array.

Method 1: Using foreach loop and unset function

We can use foreach loop to traverse the entire array and use unset function to delete non-empty elements. Here's how it's done:

foreach ($arr as $key => $value) {
    if ($value !== "" && $value !== null) {
        unset($arr[$key]);
    }
}
Copy after login

In the above code, we use a foreach loop to iterate through the entire array. If the value of the element is not an empty string or null, use the unset function to delete it.

Method 2: Use array_filter function

Another method is to use PHP’s built-in function array_filter. This function accepts an array and returns a new array containing only the elements for which the callback returns true. We can define a callback function that verifies whether the element is empty or null. Here's how it's implemented:

function emptyFilter($value) {
    return $value !== "" && $value !== null;
}

$arr = array_filter($arr, "emptyFilter");
Copy after login

In the above code, we define a callback function named emptyFilter. This function accepts a value and returns true or false. If the value is an empty string or null, the function returns false. Next, we use the array_filter function to filter the array and keep only the elements where the callback function returns true.

Summary

We have introduced two common methods for removing non-empty elements from an array. Using a foreach loop and the unset function may not be efficient when working with large arrays, as looping and deletion operations may affect performance. On the other hand, using the array_filter function is more scalable and flexible, and the filtering criteria can be customized by passing different callback functions. We hope this article helps you get better at handling arrays during PHP development.

The above is the detailed content of php remove non-empty elements from array. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template