Arrays are fundamental data structures used to store collections of elements. In many programming tasks, especially when dealing with user inputs or external data sources, arrays may contain empty elements that need to be removed for further processing. This tutorial will guide you through the process of removing empty elements from an array in PHP using various techniques.
Understanding Empty Elements
In PHP, an element is considered "empty" if it evaluates to FALSE
when converted to a boolean value. According to PHP’s type juggling rules, the following values are considered empty:
- An empty string (
''
) - The number zero (
0
) or0.0
- The string
'0'
- An array with zero elements (
[]
) NULL
- A variable declared without a value
Given this definition, when cleaning up an array to remove empty elements, you must consider these values and how they are treated.
Using array_filter()
The simplest method for removing empty elements from an array is by using the built-in PHP function array_filter()
. This function iterates over each element of an array and removes those that evaluate to FALSE
when converted to a boolean, unless you provide a custom callback function that specifies additional filtering criteria.
Basic Usage
$linksArray = ['', 'http://example.com', '', 0, '0', null];
$cleanedArray = array_filter($linksArray);
print_r($cleanedArray);
The output will be:
Array
(
[1] => http://example.com
[4] => 0
)
In the example above, all empty strings, NULL
, and numeric zero values are removed. Note that '0'
is not removed because it is a non-empty string.
Preserving Specific Values
If you need to preserve certain values such as the string '0'
, or an integer 0
, you can provide a custom callback function to array_filter()
:
$linksArray = ['', 'http://example.com', '', 0, '0', null];
$cleanedArray = array_filter($linksArray, function ($value) {
return !is_null($value) && $value !== '';
});
print_r($cleanedArray);
This will keep all elements that are not NULL
or empty strings:
Array
(
[1] => http://example.com
[3] => 0
[4] => 0
)
Handling Whitespace
Sometimes, an element might appear non-empty due to spaces. In such cases, you should first trim the elements before filtering:
$linksArray = ['', 'http://example.com ', ' ', 0, '0', null];
$trimmedArray = array_map('trim', $linksArray);
$cleanedArray = array_filter($trimmedArray);
print_r($cleanedArray);
The output will exclude all elements that are empty strings or contain only whitespace:
Array
(
[1] => http://example.com
)
Using strlen
as a Callback
For arrays containing string values, it’s essential to ensure '0'
is not considered empty. A common approach is using the strlen()
function as a callback to determine if strings are non-empty:
$linksArray = ['', 'http://example.com', '', 0, '0', null];
$cleanedArray = array_filter($linksArray, 'strlen');
print_r($cleanedArray);
This approach effectively removes only genuinely empty strings and NULL
, while keeping '0'
:
Array
(
[1] => http://example.com
[4] => 0
)
Reindexing the Array
When removing elements from an array, keys may become non-sequential. To ensure a clean, zero-indexed array, use array_values()
after filtering:
$linksArray = ['', 'http://example.com', '', 0, '0', null];
$cleanedArray = array_values(array_filter($linksArray));
print_r($cleanedArray);
The result will be a reindexed array with no gaps in the keys:
Array
(
[0] => http://example.com
[1] => 0
[2] => 0
)
Conclusion
Removing empty elements from arrays is a common task that can be efficiently handled using PHP’s array_filter()
function. By understanding how different types of values are treated as "empty" in PHP, and by applying the appropriate techniques to filter your data, you can maintain clean and reliable datasets throughout your applications.