How to remove empty values from an array in PHP
Answer: Using array_filter()
function
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, zeros, false, null values, and an empty array (arr()) from the array.
The array_filter()
function uses the callback function to filter the values from the array. This function passes each value of the input array to the callback function. If the callback function returns true, then that value will be added to the returned array otherwise, it will be removed from the array.
Example: Using array_filter()
function
In the given example, we have removed the empty values from the array $proglang using the array_filter()
function.
<!DOCTYPE html>
<html>
<head>
<title>Remove empty values from an array</title>
</head>
<body>
<?php
$proglang = array("HTML", "CSS", "JavaScript", "", "PHP", "");
$newarr = array_filter($proglang);
print_r($newarr);
?>
</body>
</html>
Array ( [0] => HTML [1] => CSS [2] => JavaScript [4] => PHP )
Using array_diff()
function
Apart from the array_filter()
function, we can also remove the empty values from the array using the 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 in other arrays.
Example: Using array_diff()
function
In the given example, we have removed the empty values from the array $proglang using array_diff()
function and store the returned value in the new array named $newarr.
<!DOCTYPE html>
<html>
<head>
<title>Remove empty values from an array</title>
</head>
<body>
<?php
$proglang = array("HTML", "CSS", "JavaScript", "", "PHP", "", "jQuery");
$newarr = array_diff($proglang, array(""));
print_r($newarr);
?>
</body>
</html>
Array ( [0] => HTML [1] => CSS [2] => JavaScript [4] => PHP [6] => jQuery )
Using unset()
Function
We can also remove the empty value from an array using the PHP unset()
function. The unset() function unset the variable. If the unset()
function is called inside the user-defined function, then it can unset the value of the local variable.
With the help of this function, we can remove the empty values from the array. This function takes only one parameter, which is the variable name whose value we want to remove.
Example: Using unset() function
In the given example, we have removed the empty values from the array $proglang using the unset()
function.
<!DOCTYPE html>
<html>
<head>
<title>Remove empty values from an array</title>
</head>
<body>
<?php
$proglang = array("HTML", "CSS", "JavaScript", "", "PHP", "", "jQuery");
foreach($proglang as $key => $newarr)
{
if($newarr === '')
{
unset($proglang[$key]);
}
}
print_r($proglang);
?>
</body>
</html>
Array ( [0] => HTML [1] => CSS [2] => JavaScript [4] => PHP [6] => jQuery )
Conclusion
In this lesson, we have learned how to remove the empty value from the array. PHP offers several built-in functions with the help of which we can remove the empty values from the array. At first, we have used the array_filter()
function to remove the empty values from the array. After that, we have used the array_diff()
function to remove the empty values from the array. Then we have used the unset()
function. This function unset the value of the variable, but can also remove the empty values from the array.