44 Votes

PHP: Remove all empty elements from string array

Tip by Stefan Trost | Last update on 2023-02-01 | Created on 2013-01-10

In this tutorial I would like to show you how you can extinguish all empty string elements from an array.

First, we look at a general solution for the deletion of array elements before we come to an easily implementable solution for removing empty strings from an array. Finally, we expand this solution with a whitespace filter in order to delete also those elements from the array that only contain spaces, for example.

General Function for deleting Array Elements

To delete certain items from an array via their index, you can operate with the function unset(), for example:

$arr = array('a', 'b', 'c');
unset($arr[1]);  // deletes 'b' from array

Since the counting of the array indices begins at 0, we delete the second element from the array with "1" here.

To erase all empty elements from an array consisting of strings, we can go through all items and remove the unwanted ones with unset():

$arr = array('a', '', 'b', '', 'c', '', '');
 
for ($i = count($arr) - 1; $i >= 0; $i--) {
   if ($arr[$i] == '') unset($arr[$i]);
}
 
// array now only contains 'a', 'b' and 'c'

We go through the array backwards, since the deletion may change the length of the array continuously. Within the loop, we check each element for an empty string and call unset() if this applies.

Delete empty Elements from a String Array - The simple Way

However, there is still a much simpler solution:

$arr = array('a', '', 'b', '', 'c', '', '');
 
$arr = array_filter($arr);
// array contains 'a', 'b' and 'c'

The function array_filter() returns a new array in that all elements have been removed, for which an optional callback function returns "false". Since we do not have specified such a callback function (as second parameter) and an empty string (as well as null or false) returns "false" in PHP automatically, we can use array_filter() in this way here, for deleting all empty elements.

Filter Whitespace, too - The extended Way

If we also want to throw a string out of the array if it only contains whitespace, for example only spaces, tabs or line breaks, we can expand our first example that works with unset() in the following way:

$arr = array('a', '', 'b', ' ', 'c', '', '     ');
 
for ($i = count($arr) - 1; $i >= 0; $i--) {
   if (trim($arr[$i]) == '') unset ($arr[$i]);
}
 
// array contains 'a', 'b' and 'c'

Instead of checking an array element directly, we first pass the element to the trim() function, which cuts all whitespace from the front and from the back of the string. As a result, every element that consists only of whitespace turns into an empty element, which is then deleted.

Of course, we also want to expand our second example that works with array_filter() with the whitespace functionality. To do this, we use a callback function that we are passing to array_filter() as a second parameter:

function checkElement($var) {
   return trim($var) != '';
}
 
 
$arr = array('a', '', 'b', ' ', 'c', '', '     ');
 
$arr = array_filter($arr, "checkElement");
// array now only contains 'a', 'b' and 'c'

We have programmed the callback function in such a way that it provides "false" for an empty string or a string that only contains whitespace. For this, we work with trim() again as in our sample code before. For all other cases, the function provides us with "true" so that the corresponding element remains in the array.

If we need other criteria to filter the array elements, we can of course further adapt and expand the callback function to our needs.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.