'How to remove empty values from multidimensional array in PHP?
I've been looking a lot of answers, but none of them are working for me.
This is the data assigned to my $quantities array:
Array(
[10] => Array([25.00] => 1)
[9] => Array([30.00] => 3)
[8] => Array([30.00] => 4)
[12] => Array([35.00] => )
[1] => Array([30.00] => )
[2] => Array([30.00] => )
)
I'm looking for a way to remove the subarrays with empty values like [12] [1] and [2] while keeping everything else.
The desired result:
Array(
[10] => Array([25.00] => 1)
[9] => Array([30.00] => 3)
[8] => Array([30.00] => 4)
)
I tried a lot of the functions on the official php docs and none of them worked.
I've used this one:
function array_filter_recursive($array, $callback = null) {
foreach ($array as $key => & $value) {
if (is_array($value)) {
$value = array_filter_recursive($value, $callback);
} else {
if ( ! is_null($callback)) {
if ( ! $callback($value)) {
unset($array[$key]);
}
} else {
if ( ! (bool) $value) {
unset($array[$key]);
}
}
}
}
unset($value);
return $array;
}
But it only removes the element in the subarrays; I need the subarrays to be removed entirely.
I don't want this:
Array(
[10] => Array([25.00] => 1)
[9] => Array([30.00] => 3)
[8] => Array([30.00] => 4)
[12] => Array()
[1] => Array()
[2] => Array()
)
Solution 1:[1]
The following function worked for my case. We can use a simple recursive function to remove all empty elements from the multidimensional PHP array:
function array_filter_recursive($input){
foreach ($input as &$value){
if (is_array($value)){
$value = array_filter_recursive($value);
}
}
return array_filter($input);
}
Then we just need to call this function:
$myArray = array_filter_recursive($myArray);
Solution 2:[2]
Not sure if this is exactly what your looking for.
function array_remove_null($array) {
foreach ($array as $key => $value)
{
if(is_null($value))
unset($array[$key]);
if(is_array($value))
$array[$key] = array_remove_null($value);
}
return $array;
}
Update (corrections):
function array_remove_null($array) {
foreach ($array as $key => $value)
{
if(is_null($value))
unset($array[$key]);
if(is_string($value) && empty($value))
unset($array[$key]);
if(is_array($value))
$array[$key] = array_remove_null($value);
if(isset($array[$key]) && count($array[$key])==0)
unset($array[$key]);
}
return $array;
}
I'm sure better checking and making this more robust might help the solution.
Solution 3:[3]
use array_map() for filter every array in $array:
$newArray = array_map('array_filter', $array);
Here's demo
Solution 4:[4]
$newArray = array_map('array_filter', $array);
This sintax it's work and very help full thank's guys ..
Solution 5:[5]
Use array_filter with array_map like below:
$arr=array_filter(array_map('array_filter', $arr));
Solution 6:[6]
This one worked for me
function array_filter_recursive($input){
foreach ($input as &$value){
if (is_array($value)){
$value = array_filter_recursive($value);
}
}
return array_filter($input);
}
Solution 7:[7]
This removes all items with null values recursively on multideminsional arrays. Works on PHP >= 5.6. If you want to remove all "empty" values, replace !is_null($value) with !empty($value).
function recursivelyRemoveItemsWithNullValuesFromArray(array $data = []): array
{
$data = array_map(function($value) {
return is_array($value) ? recursivelyRemoveItemsWithNullValuesFromArray($value) : $value;
}, $data);
return array_filter($data, function($value) {
return !is_null($value);
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Reza Mamun |
| Solution 2 | |
| Solution 3 | Naitik Shah |
| Solution 4 | Jakfar Shodiq |
| Solution 5 | lalithkumar |
| Solution 6 | jdoemax88 |
| Solution 7 |
