45

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.

user3056158
  • 707
  • 2
  • 13
  • 20

6 Answers6

82

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

jko
  • 2,068
  • 13
  • 25
29

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

print_r(array_filter($arr, 'strlen'));
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • 1
    This does not work. $ php -r "var_dump(array_filter([0, null, false, 'hi', ''], 'strlen'));" array(2) { [0] => int(0) [3] => string(2) "hi" } – Rob Dec 30 '14 at 17:19
  • 2
    this also removes '0', false, or falsey values in general might have forgotten others. – Gokigooooks Jul 28 '16 at 03:47
  • 1
    this will include '' which is not null – Nizar Blond Jan 29 '17 at 11:06
  • You can even do $arr = array_filter($arr); with no callback and it will still remove empty items. – OMA Apr 18 '18 at 12:10
  • 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` – mickmackusa May 26 '22 at 06:23
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

casraf
  • 21,085
  • 9
  • 56
  • 91
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.

Edgar Klerks
  • 1,517
  • 11
  • 21
  • 2
    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` – mickmackusa May 26 '22 at 06:24
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
)
harry
  • 1,007
  • 2
  • 10
  • 19
0

simply

$keys=array_keys($yourArray,NULL);
if(!empty($keys))
{
foreach($keys as $key)
{
unset($yourArray[$key]);
}
}
var_dump($yourarray);
Let me see
  • 5,063
  • 9
  • 34
  • 47