How to Remove Null Values From Multidimensional Array In Php

03-Apr-2023

.

Admin

Now, let's see example of how to remove null values from multidimensional array in php. you will learn remove null array from multidimensional array php. We will use remove null values array from multidimensional array php. i would like to share with you remove null array in multidimensional array php. So, let's follow few step to create example of remove null array in multidimensional array php.

Example 1:


<?php

$students = [

0 => [

'id' => 1,

'name' => 'abc'

],

1 => [

'id' => 2,

'name' => 'xyz'

],

2 =>null,

3 => [

'id' => 4,

'name' => 'mno'

],

4 => null,

5 => [

'id' => 5,

'name' => 'jkl'

],

];

$students = array_filter($students);

print_r($students);

?>

Output :

Array

(

[0] => Array

(

[id] => 1

[name] => abc

)

[1] => Array

(

[id] => 2

[name] => xyz

)

[3] => Array

(

[id] => 4

[name] => mno

)

[5] => Array

(

[id] => 5

[name] => jkl

)

)

Example 2:

<?php

$students = [

0 => [

'id' => 1,

'name' => 'abc'

],

1 => [

'id' => 2,

'name' => 'xyz'

],

2 => [

'id' => 3,

'name' => 'pqr'

],

3 => [

'id' => 4,

'name' => 'mno'

],

4 => null,

5 => [

'id' => 5,

'name' => 'jkl'

],

];

foreach ($students as $key=>$val) {

if ($val === null)

unset($students[$key]);

}

print_r($students);

?>

Output :

Array

(

[0] => Array

(

[id] => 1

[name] => abc

)

[1] => Array

(

[id] => 2

[name] => xyz

)

[2] => Array

(

[id] => 3

[name] => pqr

)

[3] => Array

(

[id] => 4

[name] => mno

)

[5] => Array

(

[id] => 5

[name] => jkl

)

)

It will help you....

#PHP