How to remove empty values from array in PHP?

In this tutorial, we will explain how to remove empty values from array in PHP without using a loop. The array_filter() function makes it easy to achieve this.

The array_filter() function filters the elements of an array using callback function. This function removes all empty values from the array.

1. Remove Empty values from array in PHP using array_filter() function

You can easily use the PHP array_filter() function to remove or filter empty values from an array.

We can remove all the empty values from the array by using the built-in array_filter() function. This function removes all the empty elements from the array.

The array_filter() function passes each value of the input array to the callback function. If the callback return true, that value will be included in the new array otherwise, it will be removed.

Let’see below example

Example 1:
<?php

  $arr_data = array("test", "testing", "", "item", "");
    
    $resultData = array_filter($arr_data, function($val) {
        return !empty($val);
    });
    
  print_r($resultData);
    

?>

Explanation:

By default, array_filter() removes elements from the array. If you want to remove specific values (like null and “” but keep 0), you can use a custom callback function.

Custom callback function
$array_value = array("test", "testing", null, "item", "");

$filteredArray = array_filter($array_value, function($value) {
    return $value !== null && $value !== "";
});
Output:
array [
    [0] => "test"
    [1] => "testing"
    [3] => "item"
]
  
Example 2:

In this given example, we have removed the empty values from the array $array_data using the array_filter() function.

<?php

   $arr_data = array("test", "testing", "", "item", "");
    
    $resultData = array_filter($arr_data);
    
    print_r($resultData);
    
?>
Output:
array [
    [0] => "test"
    [1] => "testing"
    [3] => "item"
]
  
2. Remove Empty values from array in PHP using unset() function

We can also remove empty values from array using PHP unset() function. This function unset the variable. unset() function is called inside user-defined function, then it unset the value of the local variable. We can remove the empty values from the array. Below step consider to php array remove empty values.

Example 1:

In this example, we have explain of removed the empty values from the array using the unset() function.

This function takes only one parameter, which is the variable name whose value we want to remove.

<?php

    $arr_data = array("test", "testing", "", "item", "");
    foreach($arr_data as $key => $newarray) 
    {
        if(empty($newarray)) 
        {
            unset($arr_data[$key]);
        }
    }
    
    print_r($arr_data);
    

?>

Explanation:

  1. foreach Loop: We loop througth each element of the array using foreach($arr_data as $key => $newarray).
  2. empty($newarray): The empty() function checks if the value is empty (null, false, 0, “”, ” “,  etc.).
  3. unset($arr_data[$key]): If the value is empty, unset() removes it from the array.
Output:
array [
    [0] => "test"
    [1] => "testing"
    [3] => "item"
]
  
3. Remove empty values from array in PHP using array_diff() function

In the given example, we have php remove empty values from array using array_diff() function. This function is used to compare the values of two arrays or more arrays and returns a new array having the values of the first array, which are not present other arrays. 

Example: Using array_diff() function

In this given example, we have removed the empty values from the array. array_diff() function and store the returned value in the new array. Let’see below example.

<?php

     $arr_data = array("test", "testing", "", "item", "");
    
    $resultData = array_diff($arr_data, array(""));
    
    print_r($resultData);
    
?>

You can use array_diff() to remove empty values from an array by comparing it with an array of empty values ([“”, null, false, 0, ” “]). array_diff() will return only those values that aren’t in the comparison array, effectively removing the empty ones.

$array_value = array("test", "testing", null, false, "item", "", 0, " ");

$ $resultData = array_diff($arr_data, ["", null, false, 0, " "]);
    
print_r($resultData);

Explanation:

  1. Comparison Array: [“”, null, false, 0, ” “] – This array contains values considered empty.
  2. array_diff($arr_data, [“”, null, false, 0, ” “]): Compares $arr_data with the list of empty values, and keeps only the values that don’t appear in the comparison array.
Output:
array [
    [0] => "test"
    [1] => "testing"
    [3] => "item"
]
  

I hope this tutorial help you.

Leave a Reply

Your email address will not be published. Required fields are marked *