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!
- May 21, 2008
- article by Gabriel C.
- 7 comments
Related Posts
Display Values from an Array in Random Orderat October 2, 2008
How to Remove a (non-empty) Directory using PHPat September 28, 2008 with 1 comment
Equivalent of PHP’s array_combine() functionat October 15, 2008
PHP: Make an Alphabetical Selection from Elements of an Arrayat October 5, 2008
PHP: How to select a random value from an array using a specified rangeat September 20, 2008

7 Replies to "PHP: How to remove empty values from an array"
September 25, 2008 at 3:37 PM
This is very very help for me to do dynamic checkbox with multiple textbox fields. To insert in DB.
Thanks,
Satheesh kumar.M
December 9, 2008 at 11:26 PM
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>’;
January 26, 2009 at 4:48 PM
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);
January 29, 2009 at 8:09 AM
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().
July 18, 2009 at 8:14 AM
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
December 10, 2009 at 2:43 AM
Thanks for the code, it really helped
June 26, 2010 at 11:55 PM
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 )