How to remove null values from multidimensional array in php?
In this post, we would like to share with you remove null array in multidimensional array php.
1. Code:
<?php function removeNullValuesFromMultiArray($multiArray){ $multiArray = array_filter($multiArray); return $multiArray; } $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' ], ]; print_r(removeNullValuesFromMultiArray($students)); ?>
2. Example:
Input:
$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' ], ];
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 ) )