How to Remove Empty Values From an Array in PHP?

Last updated on October 17, 2023

We can remove all empty values from the array using the built-in function array_filter(). It allow us to remove all empty elements, nulls, false, and empty arrays from an array.

The array_filter() function uses a callback function to filter values ​​from an array. It passes each value of the input array to the callback function. If the callback function returns true, that value will be added to the returned array otherwise, it will be removed from the array.

Method 01: Remove Empty Values Through array_filter() Function:

Let’s create a fruit array with both filled and empty values.

$fruits = array("apple","banana","graphs","","orange","");

This array has seven elements containing filled and empty values. The empty values need to remove, to do that we apply the array_filter() function on the $fruits array to remove the empty values.

$filtered_fruits = array_filter($fruits);

print_r($filtered_fruits);

// output: Array
(
    [0] => apple
    [1] => banana
    [2] => graphs
    [4] => orange
)

In this example, an array called $fruits is created with some empty values. The array_filter() function is then used to remove the empty values, resulting in a new array with non-empty elements.

Method 02: Remove Empty Values Through unset() Function:

We can also remove an empty value from an array using the PHP unset() function. The unset() function is used to destroy a specified variable. If the unset() function is called inside a user-defined function, it can unset the value of a local variable.

Note: it’s important to note that using array_filter() is generally a more concise and effective way to remove empty values from an array, as shown in the previous examples. unset() is more suitable for removing specific elements based on their keys rather than filtering elements based on specific criteria.

As like previous example, let’s create an array containing filled and empty values.

$subjects = array("English","Biology","Physics","","Math");

Above we created $subjects array that has five elements containing four filled and one empty value. The empty value needs to remove. To do that, we use the unset() function. Look at the below code,

unset($subjects[3]);

print_r($subjects);

// output:Array

(
    [0] => English
    [1] => Biology
    [2] => Physics
    [4] => Math
)

In this example, the $subjects array contains various subjects, including an empty value. The unset($subjects[3]); statement specifically targets the empty value at index 3 (fourth element) and removes it from the array, resulting in an updated array without the empty value.

Conclusion:

This article showed how to remove empty values from an array in PHP using two methods: array_filter() and unset(). We explained the array_filter() function with an example and showcased how the unset() function can remove specific elements. These techniques help clean up arrays by eliminating empty values, enhancing data management in PHP.


Written by
I am Siraj, a results-driven web developer with three years of hands-on experience in both frontend and backend development. My passion for creating seamless digital experiences drives me to continuously innovate and excel in my field.

Share on:

Related Posts

Improved by: Ayaz Ali Shah

Tags: PHP,