How to Remove Null Values From Multidimensional Array In Php
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....
- How To Use Php Conditional Statement Example
- How to Check if String Contains Regex in PHP?
- PHP Array_column() Function Example
- PHP Generate List of Random Numbers Between 0 and 100 Example
- How to User Login with Facebook in PHP?
- How to Explode Every Character into Array Using PHP Example?
- PHP Form Validation Tutorial
- PHP Convert XML to JSON Example