PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
No Result
View All Result
Home How-to

How to remove empty array elements using PHP

Handy short guide on how to remove empty array elements using PHP.

September 18, 2022
Reading Time: 3 mins read
PHP101.Net - How to remove empty array elements using PHP

PHP101.Net - How to remove empty array elements using PHP

Share on FacebookShare on Twitter

Quick solution

The quickest way to remove empty array elements in a PHP array is using the array_filter() function:

array_filter( $array );

This function will simply loop through the array and return the elements that are not empty, achieving the result we wanted. To better understand this function, below are more detail instructions.

To remove empty array elements

Using array_filter() to remove empty array elements

The basic method to remove empty array elements in a PHP array is using array_filter() function. Assuming we have this array with various empty elements:

$colors = ['red', 'blue', '', 'green', '', 'yellow'];

We wanted to only keep the array elements containing values and remove empty ones. This can be done easily using array_filter():

// Remove all empty array elements
$colors = array_filter($colors);

// the array is now ['red', 'blue', 'green', 'yellow']

How does array_filter() work?

Based on PHP.net official documentation on array_filter() function, the function works by accepting minimum of one argument – the array to filter:

array_filter(array $array, ?callable $callback = null, int $mode = 0)
  • $array is the array we want to filter
  • $callback is the callback function to loop through the array. If no $callback is supplied, all empty entries of array will be removed. That’s how empty array elements are removed.

However, sometimes this function doesn’t work as expected – the assuming empty elements aren’t removed. Why is it? Because it depends on how PHP treats what is empty with the empty() function. There are blank Unicode values that are a spacing, null values that are not treated as empty, therefore not being removed.

How can we sort this issue? That’s when extra callback is useful.

Optional callback for extra removal functionality

Using callback to remove empty array elements will help cases with elements having special characters not being treated as ’empty’, or simply as the character ‘0’ (that we wanted to remove) to be fully removed from the array.

We will create the callback function first. Based on PHP.net documentation, If the callback function returns true, the current value from array is returned into the result array. So we need to return true on the elements we want to keep, which is the valid elements:

// Function to actually keep and remove the elements we don't want
function remove_empty( $value ) {
  
  // If $value is not empty (valid), keep it (returns true), else (empty), remove it (returns false)
  // -or- if $value is not equals '0' (valid), keep it (returns true), else (is '0'), remove it (returns false)
  return !empty($value) || $value !== '0';
}

Then, we can pass the callback function into the actual array_filter() function:

$colors = array_filter( $colors, 'remove_empty' );

Tips: Additional array elements cleaning

There might be blank spaces in your array elements that aren’t counted as ’empty’ defined by PHP, which you can try to trim the spaces using trim() function with array_map() first before cleaning the array:

// Trim the array elements, removing blank spaces on all elements
$colors = array_map( 'trim', $colors );

// Do the filter
$colors = array_filter( $colors );

Final thoughts

These are the guide to remove empty array elements using PHP, with some extra tips on cleaning the array elements before filtering it. We hope that this short guide would help you using array_filter() and array_map() effectively.

You might also like

  • How to convert from array to string using PHP
  • How to add elements to array

References

  • PHP.net Official Documentation – array_filter() function
  • PHP.net Official Documentation – empty() function
Tags: array_filterarray_mapphp array_filterphp array_mapphp remove empty array elementsremove empty array elementsremove empty elements
Previous Post

How to install ImageMagick on Windows

Next Post

Deploy a webserver with XAMPP on Windows for PHP development

Related Posts

PHP101.net - How to set timezone with PHP

How to set timezone with PHP easily (multiple methods)

March 9, 2023
PHP101.net - How to redirect to another page using PHP

How to redirect to another page using PHP

March 8, 2023

Table of Contents

    • Quick solution
    • To remove empty array elements
    • Using array_filter() to remove empty array elements
    • How does array_filter() work?
    • Optional callback for extra removal functionality
    • Tips: Additional array elements cleaning
    • Final thoughts
    • You might also like
    • References
Next Post
PHP101.Net - Deploy - Webserver with XAMPP on Windows for PHP development

Deploy a webserver with XAMPP on Windows for PHP development

We deliver free and helpful resources for learning
and using PHP programming language.

About
Contact Us
Privacy Policy
Main Sections

Home
Deploy
How-to
Tutorial
You might be interested in...

How to redirect to another page using PHP

How to install Composer on Linux

How to install PHP GeoIP extension & GeoLite2 library (databases included)

Deploy a webserver with XAMPP on Windows for PHP development

How to set timezone with PHP easily (multiple methods)

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone.

No Result
View All Result
  • Home
  • Deploy
  • How-to
  • Tutorial

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone. | Privacy Policy

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.