从 PHP 数组中删除零值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2287404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:53:48  来源:igfitidea点击:

Remove zero values from a PHP array

phparrays

提问by Elitmiar

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 数组函数

回答by Gumbo

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

array_filter这样做。如果您不提供回调函数,它会过滤掉所有等于false布尔转换)的值。

回答by Jon Winstanley

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

您可以循环遍历数组并取消设置完全等于 0 的任何项目

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

回答by thecodedeveloper.com

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_difffunction

第二种方法:使用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>";
  ?>

回答by Slav

bit late, but copy & paste:

有点晚了,但复制并粘贴:

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

回答by Amir Fo

You can use this:

你可以使用这个:

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

回答by Geoffray Warnants

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)

回答by krez

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

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

如果要删除零空值,正确的代码是:

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

回答by Jumper Pot

This one is also an effective solution to remove unwanted value.

这也是删除不需要的值的有效解决方案。

 <?php
   $array = array(0,100,0,150,0,200);
   foreach($array as $a){    
   if (false !== $key = array_search("0", $array)){        
   unset($array[$key]);    
   }    
   }    
   print_r($array);    
   ?>