46

I have an array

Array ( [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.

2
  • php.net/array_filter with custom callback. Commented Dec 19, 2013 at 8:35
  • on php.net look up array filter Commented Dec 19, 2013 at 8:35

6 Answers 6

85

this will do the trick:

array_filter($arr, static function($var){return $var !== null;} );

Code Example: https://3v4l.org/jtQa2


for older versions (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

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

7 Comments

Only works in newer versions, but this is way better than the strange strlen thing.
For older versions one can predeclare the function used.
You can exclude the second param entirely; array_filter($arr); will remove null by default.
@AlexP nope, you are wrong. as seen on 3v4l.org/Y9KCX your solution would remove everything that is empty() - not just NULL.
@AlexP: a string '0' for instance will be removed, if you dismiss filter function.
|
29

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

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

5 Comments

This does not work. $ php -r "var_dump(array_filter([0, null, false, 'hi', ''], 'strlen'));" array(2) { [0] => int(0) [3] => string(2) "hi" }
this also removes '0', false, or falsey values in general might have forgotten others.
this will include '' which is not null
You can even do $arr = array_filter($arr); with no callback and it will still remove empty items.
From PHP8.1 this is no longer a good technique because it will generate Deprecations. Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated
6

You can just loop through it.

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

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

Comments

4

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:

 $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(NULL) will be strlen("")->0, thus false
  • strlen("") will be strlen(("")->0, thus false etc.

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.

1 Comment

From PHP8.1 strlen is no longer a good technique because it will generate Deprecations. Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated
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
)

Comments

0

simply

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.