0

I want to remove empty elements from my array.

I found this code for that :

$outaa = array_map('trim', $outaa);
$outaa = array_filter($outaa);

My array contains elements whose contain '0' only. My code removes those elements too which only have '0' in it. but i dont want to remove those.

1
  • Define empty. "", 0, 0.0, "0", NULL, FALSE, array()? Commented Feb 1, 2012 at 11:48

3 Answers 3

1
$outaa = array_remove_empty($outaa);


function array_remove_empty($arr){
        $narr = array();
        while(list($key, $val) = each($arr)){
            if (is_array($val)){
                $val = array_remove_empty($val);
                // does the result array contain anything?
                if (count($val)!=0){
                    // yes :-)
                    $narr[$key] = $val;
                }
            }
            else {
                if (trim($val) != ""){
                    $narr[$key] = $val;
                }
            }
        }
        unset($arr);
        return $narr;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

array_filters removes empty elements in the non-strict sense. Ie it will remove 0, '', null, array(), etc.

You have to use a function that does strict comparison. I assume that "empty" meant null and that you have PHP 5.3+ (for the use of closures):

$outaa = array_filter($outaa, function($element) { 
    if($element !== null) return true;
});

Or if you don't have 5.3 use a normal callback:

function filter_empty($element) 
{ 
    if($element !== null) return true; 
} 
$outaa = array_filter($outaa, 'filter_empty');

2 Comments

Error : PHP Parse error: syntax error, unexpected '}' in /var/www/server/rcon.php on line 29
You can also do this with a normal callback instead of a closure: function filter_empty($element) { if($element !== null) return true; } $outaa = array_filter($outaa, 'filter_empty'); That will work with earlier vbersions of php.
0

One-liners are always nice. The following will remove all empty strings, NULL, and FALSE elements from an array:

$clean_array = array_diff(array_map('trim', $my_array), array('', NULL, FALSE));

Explanation:

  • 1st parameter of array_diff: The trimmed version of $my_array. Using array_map, surrounding whitespace is trimmed from every element via the trim function. It is good to use the trimmed version in case an element contains a string that is nothing but whitespace (i.e. tabs, spaces), which I assume would also want to be removed. You could just as easily use $my_array for the 1st parameter if you don't want to trim the elements.
  • 2nd parameter of array_diff: An array of items that you would like to remove from $my_array.
  • Output: An array of elements that are contained in the 1st array that are not also contained in the 2nd array. In this case, because '', NULL, and FALSE are within the 2nd array, they can never be returned by array_diff.

EDIT:

It turns out you don't need to have NULL and FALSE in the 2nd array. Instead you can just have '', and it will work the same way:

$clean_array = array_diff(array_map('trim', $my_array), array(''));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.