如何从数组中移除空值?(复制)

时间:2022-08-26 14:54:21

This question already has an answer here:

这个问题已经有了答案:

I have an array

我有一个数组

Array ( [0] => 0 [1] => [2] => 3 [3] => )

([0] => 0 [1] => [2] => 3 [3] =>)

i want to remove null values from this and the result should be like this

我想从这里删除空值,结果应该是这样的

Array ( [0] => 0 [1] => 3) i don't want to remove 0 value from array.

数组([0]=> 0[1]=> 3)我不想从数组中删除0值。

6 个解决方案

#1


51  

this will do the trick:

这将会达到目的:

array_filter($arr, function($var){return !is_null($var);} );

Code Example: http://3v4l.org/icEDa

代码示例:http://3v4l.org/icEDa


for older versions (php<5.3):

旧版本(php < 5.3):

function is_not_null ($var) { return !is_null($var); }
$filtered = array_filter($arr, 'is_not_null');

Code Example: http://3v4l.org/CKrYO

代码示例:http://3v4l.org/CKrYO

#2


20  

You can use array_filter() which will get rid of the null empty values from the array

可以使用array_filter()从数组中删除空值

print_r(array_filter($arr, 'strlen'));

#3


5  

You can just loop through it.

你可以循环它。

<?php 
foreach ($array as $i=>$row) {
    if ($row === null)
       unset($array[$i]);
}

CodePad

CodePad

If you want to reindex the array to remove gaps between keys, you can just use a new array:

如果你想重新索引数组以消除键之间的间隔,你可以使用一个新的数组:

<?php
$array2 = array();
foreach ($array as $row) {
    if ($row !== null)
       $array2[] = $row;
}
$array = $array2;

CodePad

CodePad

#4


2  

<?php 
$arr = array( 0 => 0, 1=>null, 2=>3, 3=>null); 
foreach ($arr as $key=>$val) {
    if ($val === null)
       unset($arr[$key]);
}
$new_arr = array_values($arr);
print_r($new_arr);
?>

Out put:

把:

Array
(
    [0] => 0
    [1] => 3
)

#5


1  

You are in a world of trouble now, because it is not too easy to distinguish null from 0 from false from "" from 0.0. But don't worry, it is solvable:

您现在正处于一个麻烦的世界,因为区分零和0、假和0和“”是不太容易的。但别担心,这是可以解决的:

 $result = array_filter( $array, 'strlen' );

Which is horrible by itself, but seems to work.

这本身就很可怕,但似乎行得通。

EDIT:

编辑:

This is bad advice, because the trick leans on a strange corner case:

这是一个糟糕的建议,因为这个技巧是基于一个奇怪的情况:

  • strlen(0) will be strlen("0") -> 1, thus true
  • strlen(0)将是strlen(“0”)-> 1,因此为真
  • strlen(NULL) will be strlen("")->0, thus false
  • strlen(NULL)将是strlen(")->,因此为false
  • strlen("") will be strlen(("")->0, thus false etc.
  • strlen(")将是strlen(")->,因此为false等。

The way you should do it is something like this:

你应该这样做:

 $my_array = array(2, "a", null, 2.5, NULL, 0, "", 8);

 function is_notnull($v) {
    return !is_null($v);
  }

 print_r(array_filter($my_array, "is_notnull"));

This is well readable.

这是可读的。

#6


0  

simply

简单的

$keys=array_keys($yourArray,NULL);
if(!empty($keys))
{
foreach($keys as $key)
{
unset($yourArray[$key]);
}
}
var_dump($yourarray);

#1


51  

this will do the trick:

这将会达到目的:

array_filter($arr, function($var){return !is_null($var);} );

Code Example: http://3v4l.org/icEDa

代码示例:http://3v4l.org/icEDa


for older versions (php<5.3):

旧版本(php < 5.3):

function is_not_null ($var) { return !is_null($var); }
$filtered = array_filter($arr, 'is_not_null');

Code Example: http://3v4l.org/CKrYO

代码示例:http://3v4l.org/CKrYO

#2


20  

You can use array_filter() which will get rid of the null empty values from the array

可以使用array_filter()从数组中删除空值

print_r(array_filter($arr, 'strlen'));

#3


5  

You can just loop through it.

你可以循环它。

<?php 
foreach ($array as $i=>$row) {
    if ($row === null)
       unset($array[$i]);
}

CodePad

CodePad

If you want to reindex the array to remove gaps between keys, you can just use a new array:

如果你想重新索引数组以消除键之间的间隔,你可以使用一个新的数组:

<?php
$array2 = array();
foreach ($array as $row) {
    if ($row !== null)
       $array2[] = $row;
}
$array = $array2;

CodePad

CodePad

#4


2  

<?php 
$arr = array( 0 => 0, 1=>null, 2=>3, 3=>null); 
foreach ($arr as $key=>$val) {
    if ($val === null)
       unset($arr[$key]);
}
$new_arr = array_values($arr);
print_r($new_arr);
?>

Out put:

把:

Array
(
    [0] => 0
    [1] => 3
)

#5


1  

You are in a world of trouble now, because it is not too easy to distinguish null from 0 from false from "" from 0.0. But don't worry, it is solvable:

您现在正处于一个麻烦的世界,因为区分零和0、假和0和“”是不太容易的。但别担心,这是可以解决的:

 $result = array_filter( $array, 'strlen' );

Which is horrible by itself, but seems to work.

这本身就很可怕,但似乎行得通。

EDIT:

编辑:

This is bad advice, because the trick leans on a strange corner case:

这是一个糟糕的建议,因为这个技巧是基于一个奇怪的情况:

  • strlen(0) will be strlen("0") -> 1, thus true
  • strlen(0)将是strlen(“0”)-> 1,因此为真
  • strlen(NULL) will be strlen("")->0, thus false
  • strlen(NULL)将是strlen(")->,因此为false
  • strlen("") will be strlen(("")->0, thus false etc.
  • strlen(")将是strlen(")->,因此为false等。

The way you should do it is something like this:

你应该这样做:

 $my_array = array(2, "a", null, 2.5, NULL, 0, "", 8);

 function is_notnull($v) {
    return !is_null($v);
  }

 print_r(array_filter($my_array, "is_notnull"));

This is well readable.

这是可读的。

#6


0  

simply

简单的

$keys=array_keys($yourArray,NULL);
if(!empty($keys))
{
foreach($keys as $key)
{
unset($yourArray[$key]);
}
}
var_dump($yourarray);