Recursively Remove Empty Elements and Subarrays from a Multi-Dimensional Array

Recursively remove empty elements and subarrays from a multi-dimensional array

Try using array_map() to apply the filter to every array in $array:

$array = array_map('array_filter', $array);
$array = array_filter($array);

Demo: http://codepad.org/xfXEeApj

Remove empty arrays from multidimensional array

try this- This will remove empty arrays as well inside array!

$array['recaptcha_response_field'] = array(
'name'=>'name1',
'email'=>'email1',
'empty'=>''
);
$array['terms'] = array(
'name'=>'name2',
'email'=>'email2',
'empty'=>''
);

$array['terms2'] = array();

$array= array_filter(array_map('array_filter', $array));

print_r($array);

OUTPUT-Array
(
[recaptcha_response_field] => Array
(
[name] => name1
[email] => email1
)

[terms] => Array
(
[name] => name2
[email] => email2
)

)

How to remove empty values from multidimensional array in PHP?

Bit late, but may help someone looking for same answer. I used this very simple approach to;

  1. remove all the keys from nested arrays that contain no value, then
  2. remove all the empty nested arrays.

$postArr = array_map('array_filter', $postArr);
$postArr = array_filter( $postArr );

How to remove empty values from the multi dimensional array

if you want to remove the index 4 that is an empty array :

array_filter(array_map('array_filter', $array)); 

PHP - How to remove empty entries of an array recursively?

Try this code:

<?php
function array_remove_empty($haystack)
{
foreach ($haystack as $key => $value) {
if (is_array($value)) {
$haystack[$key] = array_remove_empty($haystack[$key]);
}

if (empty($haystack[$key])) {
unset($haystack[$key]);
}
}

return $haystack;
}

$raw = array(
'firstname' => 'Foo',
'lastname' => 'Bar',
'nickname' => '',
'birthdate' => array(
'day' => '',
'month' => '',
'year' => '',
),
'likes' => array(
'cars' => array('Subaru Impreza WRX STi', 'Mitsubishi Evo', 'Nissan GTR'),
'bikes' => array(),
),
);

print_r(array_remove_empty($raw));

Remove items from multidimensional array in PHP

Try this Function. This will solve your issue.

 function cleanArray($array)
{
if (is_array($array))
{
foreach ($array as $key => $sub_array)
{
$result = cleanArray($sub_array);
if ($result === false)
{
unset($array[$key]);
}
else
{
$array[$key] = $result;
}
}
}

if (empty($array))
{
return false;
}

return $array;
}

How to remove null values in multi-dimensional array?

Here we are using foreach to iterate over values and using $value for non-empty $value["fcmToken"]

$result=array();
foreach($array as $key => $value)
{
if(!empty($value["fcmToken"]))
{
$result[]=$value;
}
}

print_r($result);

Output:

Array
(
[0] => Array
(
[fcmToken] => dfqVhqdqhpk
)

[1] => Array
(
[fcmToken] => dfgdfhqdqhpk
)

)

How to remove empty values from multidimensional array in PHP?

Bit late, but may help someone looking for same answer. I used this very simple approach to;

  1. remove all the keys from nested arrays that contain no value, then
  2. remove all the empty nested arrays.

$postArr = array_map('array_filter', $postArr);
$postArr = array_filter( $postArr );

Can't remove empty keys from multidimensional array

You can use some array functions to remove keys that is empty.

$arr_a = array("AAA","","CCC","");

$arr_b = array("a1","b1","a2","b2","a3","b3","a4","b4");

$arr_c = array_chunk($arr_b,2);

$c = array_combine($arr_a,$arr_c);

$limp= array_filter($c);

$filteredkeys = array_filter(array_keys($limp)); // here i remove the "" key

$filtered = array_intersect_key($limp, array_flip($filteredkeys)); // since $filterdkeys is values i need to flip it and intersect with $limp
var_dump($filtered);

output:

array(2) {
["AAA"]=>
array(2) {
[0]=>
string(2) "a1"
[1]=>
string(2) "b1"
}
["CCC"]=>
array(2) {
[0]=>
string(2) "a3"
[1]=>
string(2) "b3"
}
}

https://3v4l.org/miNeW



Related Topics



Leave a reply



Submit