从PHP数组中删除零值

时间:2010-02-18 09:10:15

标签: php arrays

我有像这样的普通数组

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
)

我需要删除此数组中的所有0,这是否可以使用PHP数组函数

9 个答案:

答案 0 :(得分:79)

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

答案 1 :(得分:8)

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

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

答案 2 :(得分:5)

第一种方法:

<?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>";
    }
?>

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

答案 3 :(得分:4)

有点晚了,但副本&amp;粘贴:

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

答案 4 :(得分:1)

如果你不关心保留数据关联的关键,你可以使用这个单行技巧:

<?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)

答案 5 :(得分:1)

您可以使用此:

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

答案 6 :(得分:0)

create function fn_title_case
    (v_input_string varchar(255))
RETURNS varchar(255)
BEGIN

declare i int;
DECLARE punctuation varchar(17);
declare this_character char(1);
declare output_string varchar(255);
declare boolean_value int;
declare input_length int;

set input_length = char_length(v_input_string);
set boolean_value = 0;
set i = 0;
set punctuation = ' ';
set output_string = '';

begin
-- Loop through each character in the input string
while i <= input_length do
    -- Set the current substring to position i with length 1
    SET this_character = SUBSTRING( v_input_string, i, 1 );
    -- If the current character exists in the punctuation string, i.e. if the current character is a space
    IF LOCATE( this_character, punctuation ) > 0 THEN
        -- Set the current character to null and the boolean to 1
        set this_character = '', output_string = concat(output_string, this_character), boolean_value = 1;
    elseif boolean_value=1 then
        -- Only if the punctuation (a space) was detected in the previous step, 
        -- Insert a space and capitalize the letter
        SET output_string = concat(output_string, ' ', UCASE(this_character)), boolean_value = 0;
    else
        -- If the space was NOT detected, add the current character in lower case
        set output_string = concat(output_string, LCASE(this_character));
    end if;
    set i = i+1;
end while;
end;
    RETURN output_string;

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

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

答案 7 :(得分:0)

在php中简单地从数组中删除空值或0值

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

答案 8 :(得分:-1)

这也是消除不必要的价值的有效解决方案。

 <?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);    
   ?>