从PHP数组中删除0个值

时间:2021-06-27 13:39:52

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

我需要从这个数组中删除所有的0,这对PHP数组函数是否可行

5 个解决方案

#1


69  

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

array_filter呢。如果你不提供回调函数,它会过滤掉所有的值。

#2


7  

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

你可以对数组进行循环,并取消对任何等于0的项的设置

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

#3


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

第二种方法:使用array_diff函数

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

#4


4  

bit late, but copy & paste:

位迟,但复制粘贴:

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

#5


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


69  

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

array_filter呢。如果你不提供回调函数,它会过滤掉所有的值。

#2


7  

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

你可以对数组进行循环,并取消对任何等于0的项的设置

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

#3


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

第二种方法:使用array_diff函数

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

#4


4  

bit late, but copy & paste:

位迟,但复制粘贴:

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

#5


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)