How to Remove Null Values From Array in PHP?

03-Apr-2023

.

Admin

How to Remove Null Values From Array in PHP?

Hi Dev,

This article will give you example of how to remove null values from array in PHP. we will help you to give example of program to remove null array elements in php. you can see remove null values from array php with code examples. you will learn array_filter() function use remove null values in php. you will do the following things for php array remove all null values.

A convenient method to remove all null values from an array in PHP is by using the array_filter() function.

Example 1:


index.php

<?php

$example = array(50, "nicesnippets", null, "mywebtuts", NULL, "", 100);

//Get remove null values from array

$result = array_filter($example, function($value) {return !is_null($value);});

print_r($result);

?>

Output:

Array

(

[0] => 50

[1] => nicesnippets

[3] => mywebtuts

[5] =>

[6] => 100

)

I hope it could help you...

#PHP