51

I have a normal array like this

Array
(
    [0] => 0
    [1] => 150
    [2] => 0
    [3] => 100
    [4] => 0
    [5] => 100
    [6] => 0
    [7] => 100
    [8] => 50
    [9] => 100
    [10] => 0
    [11] => 100
    [12] => 0
    [13] => 100
    [14] => 0
    [15] => 100
    [16] => 0
    [17] => 100
    [18] => 0
    [19] => 100
    [20] => 0
    [21] => 100
)

I need to remove all 0's from this array, is this possible with a PHP array function

7 Answers 7

105

array_filter does that. If you don’t supply a callback function, it filters all values out that equal false (boolean conversion).

Sign up to request clarification or add additional context in comments.

2 Comments

Nice solution. I suppose my answer remains useful for anyone who has this issue but the value to remove is non-zero.
A callback can be used with array_filter() for when the filtering needs to be customised.
12

bit late, but copy & paste:

$array = array_filter($array, function($a) { return ($a !== 0); });

Comments

9

You can use this:

$result = array_diff($array, [0]);   

Comments

8

You can just loop through the array and unset any items that are exactly equal to 0

foreach ($array as $array_key => $array_item) {
  if ($array[$array_key] === 0) {
    unset($array[$array_key]);
  }
}

2 Comments

its not working if use ===, buts working if i use ==
I wouldn't write $array[$array_key] inside the condition, I'd write $array_item because it is simpler.
4

First Method:

<?php
    $array = array(0,100,0,150,0,200);
    
    echo "<pre>";
    print_r($array);
    echo "</pre>";
    
    foreach($array as $array_item){
            if($array_item==0){
               unset($array_item);
        }
        echo"<pre>";
        print_r($array_item);
        echo"</pre>";
    }
?>

Second Method: Use array_diff function

  <?php
    $array = array(0,100,0,150,0,200);
    $remove = array(0);
    $result = array_diff($array, $remove);                          
    echo"<pre>";
    print_r($result);
    echo"</pre>";
  ?>

Comments

4
$array = array_filter($array, function($a) { return ($a !== 0); });"

if you want to remove zero AND empty values, the right code is:

$array = array_filter($array, function($a) { return ($a !== 0 AND trim($a) != ''); });

2 Comments

Some tests for the second snippet: 3v4l.org/cc2vm
Stray " at the end of your first line.
0

If you don't care about preserving key to data correlations, you can use this single line trick :

<?php
$a = array(0, 150, 0, 100, 0, 100, 0, 100);

$b = explode('][', trim(str_replace('[0]', '', '['.implode('][', $a).']'), '[]'));

print_r($b); // Array ([0] => 150 [1] => 100 [2] => 100 [3] => 100)

1 Comment

This is not optimized, but +1 for your try!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.