How to Remove Empty Elements from an Array in PHP: A Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Empty Elements in an Array Are a Headache

\n\n

Every PHP developer sooner or later encounters a situation where an array that should contain only useful data ends up cluttered with "junk": empty strings, null values, false, or zeros. This happens especially often when processing data from forms, CSV files, API responses, or when working with user input.

\n\n

Imagine: you receive an array of tags from a user, and half of them are empty strings. Or you import data from Excel, and some cells contain only a space. If you don't clean such an array, the results can be disastrous: incorrect database queries, broken filtering logic, extra entries in reports.

\n\n

In this article, we will explore how to remove empty elements from an array in PHP as efficiently as possible. You will learn not only basic methods but also advanced techniques: recursive cleaning, working with multidimensional arrays, and fine-tuning filtering. Let's go!

\n\n

1. Basic Method: array_filter() — Your Main Tool

\n\n

The most popular and concise way to remove empty elements from a PHP array is the array_filter() function. By default, it removes all elements that are considered "falsy" in a boolean context (false):

\n\n
    \n
  • "" (empty string)
  • \n
  • null
  • \n
  • false
  • \n
  • 0 and "0"
  • \n
  • [] (empty array)
  • \n
\n\n
<?php\n$data = ['apple', '', 'banana', null, 'cherry', false, 0];\n$clean = array_filter($data);\nprint_r($clean);\n// Result: Array ( [0] => apple [2] => banana [4] => cherry )\n?>
\n\n

Note that the array keys are preserved. This can be a problem if you need sequential numbering. The solution is to wrap the result in array_values():

\n\n
<?php\n$clean = array_values(array_filter($data));\nprint_r($clean);\n// Result: Array ( [0] => apple [1] => banana [2] => cherry )\n?>
\n\n

1.1. Fine-Tuning: How to Remove Only Empty Strings but Keep Zeros

\n\n

What if you need to remove empty strings from a PHP array but keep numbers, including zero? array_filter() without parameters will remove zeros too. Use a callback function:

\n\n
<?php\n$data = ['apple', '', 'banana', 0, 'cherry', '   '];\n$clean = array_filter($data, function($value) {\n    return $value !== '' && $value !== null;\n});\nprint_r($clean);\n// Result: Array ( [0] => apple [2] => banana [3] => 0 [4] => cherry [5] =>    )\n?>
\n\n

But spaces remain! To remove them as well, use trim():

\n\n
<?php\n$clean = array_filter($data, function($value) {\n    return trim($value) !== '' && $value !== null;\n});\nprint_r($clean);\n// Result: Array ( [0] => apple [2] => banana [3] => 0 [4] => cherry )\n?>
\n\n

2. Advanced Methods: array_diff() and array_unique()

\n\n

Sometimes you need to remove empty strings from a PHP array or specific values without using a callback. array_diff() — the array comparison function — comes to the rescue:

\n\n
<?php\n$data = ['apple', '', 'banana', null, 'cherry', false];\n// Create an array of "stop values"\n$remove = ['', null, false];\n$clean = array_diff($data, $remove);\nprint_r($clean);\n// Result: Array ( [0] => apple [2] => banana [4] => cherry )\n?>
\n\n

The advantage of this method is clarity. You explicitly specify which values you consider "empty." But remember: array_diff() does not remove elements by keys; it compares values.

\n\n

2.1. Removing Duplicate Empty Values

\n\n

If the array contains several identical empty

Blogs

Book Recommendations