» Birthday Bundle - Over $400 worth of Envato files for just $20

PHP: How to remove empty values from an array

Posted on May 21, 2008, Filled under PHP,  Bookmark it

This function removes empty values from an array. This is useful if you’re working with dynamical arrays (for instance data harvested from a web site).

<?php
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/web-programming/php/remove-empty-values-from-an-array.html
*/
function remove_array_empty_values($array, $remove_null_number = true)
{
	$new_array = array();

	$null_exceptions = array();

	foreach ($array as $key => $value)
	{
		$value = trim($value);

        if($remove_null_number)
		{
	        $null_exceptions[] = '0';
		}

        if(!in_array($value, $null_exceptions) && $value != "")
		{
            $new_array[] = $value;
        }
    }
    return $new_array;
}
?>

Example of usage:

$array = array("white", "yellow", 0, "green", " ", "navy");

$remove_null_number = true;
$new_array = remove_array_empty_values($array, $remove_null_number);

echo '<pre>'; print_r($new_array); echo '</pre>';

The new array will look like this:

Array
(
    [0] => white
    [1] => yellow
    [2] => green
    [3] => navy
)

NOTE: If you wish to keep the null number (0) then set the variable $remove_null_number to false.

Do you wish to receive the latest updates as soon as they are posted? Get our RSS Feed or Subscribe to the Newsletter!

Get our RSS Feed!

7 Replies to "PHP: How to remove empty values from an array"

  1. This is very very help for me to do dynamic checkbox with multiple textbox fields. To insert in DB.

    Thanks,
    Satheesh kumar.M

  2. Hi, nice script!
    but instead of this long function try using the following code :

    function filter_string($str){
    if($str == "" || $str == 0){
    return false;
    }else{
    return true;
    }
    }

    $array = array("white", "yellow", 0, "green", " ", "navy");

    $filtered = array_filter($array,’filter_string’);

    echo ‘<pre>’; print_r($filtered ); echo ‘</pre>’;

  3. To make it even easier, PHP's built-in array_filter() function automatically strips any array values that equate to FALSE (which empty strings do). Thus, all you really need is:

    array_filter($array);

  4. Seth, I know about array_filter(). However, my function can keep the '0' values (if specified) and also removes the unnecessary whitespace from the string by using trim().

  5. Nice solution, it’s good to have a function doing that, in particular if you would like to do something on array elements rather than simply removing empty elements!

    To simple remove empty elements there’s a really quick solution, only one line of code:

    $SweepedArray = array_values(array_filter($InitialArray));

    Check out the full story here:
    http://www.xomba.com/php_how_delete_empty_values_array_single_line_code

  6. Thanks for the code, it really helped :)

  7. IMHO, this is much better:
    $new_array = array_diff(array(“white”, “yellow”, 0, “green”, ” “, “navy”), array(‘ ‘, 0));
    print_r($new_array);// Array ( [0] => white [1] => yellow [3] => green [5] => navy )

    In case if the index matters, the following extra step could be performed:
    $new_array = array_merge($new_array, array());
    print_r($new_array); // Array ( [0] => white [1] => yellow [2] => green [3] => navy )

Leave a Reply


* = required fields

  (will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: If you want to post CODE Snippets, please make them postable first!
(e.g. <br /> should be converted to &lt;br /&gt;)