Laravel - Remove elements having NULL value from multidimentional array

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 03:38:35

问题


I am using Laravel 5.3.

I have a multidimensional array like:

Array
(
    [id] => 37141
    [last_done_on] => []
    [children] => Array
        (
            [0] => NULL /* This must be removed */
            [1] => Array
                (
                    [id] => 37142
                    [last_done_on] => Array()
                    [children] => Array()
                )

            [2] => Array
                (
                    [id] => 37143
                    [last_done_on] => Array()
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 37144
                                    [last_done_on] => Array()
                                    [children] => Array()
                                )
                            [1] => Array
                                (
                                    [id] => 37145
                                    [last_done_on] => Array()
                                    [children] => Array()
                                )
                        )
                )
            [3] => Array
                (
                    [id] => 37157
                    [last_done_on] => Array()
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 37158
                                    [last_done_on] => Array()
                                    [children] => Array()
                                )
                            [1] => Array
                                (
                                    [id] => 37159
                                    [last_done_on] => Array()
                                    [children] => Array
                                        (
                                            [0] => NULL  /* This must be removed */
                                        )
                                )
                        )
                )
        )
)

And I want to remove the elements that are NULL. So the result should be like:

Array
(
    [id] => 37141
    [last_done_on] => []
    [children] => Array
        (
            [0] => Array
                (
                    [id] => 37142
                    [last_done_on] => Array()
                    [children] => Array()
                )

            [1] => Array
                (
                    [id] => 37143
                    [last_done_on] => Array()
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 37144
                                    [last_done_on] => Array()
                                    [children] => Array()
                                )
                            [1] => Array
                                (
                                    [id] => 37145
                                    [last_done_on] => Array()
                                    [children] => Array()
                                )
                        )
                )
            [2] => Array
                (
                    [id] => 37157
                    [last_done_on] => Array()
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 37158
                                    [last_done_on] => Array()
                                    [children] => Array()
                                )
                            [1] => Array
                                (
                                    [id] => 37159
                                    [last_done_on] => Array()
                                    [children] => Array
                                        (
                                        )
                                )
                        )
                )
        )
)

How to do this?


回答1:


If you want to remove the null values but not the empty arrays you could do something like:

function array_remove_null($item)
{
    if (!is_array($item)) {
        return $item;
    }

   return collect($item)
        ->reject(function ($item) {
            return is_null($item);
        })
        ->flatMap(function ($item, $key) {

            return is_numeric($key)
                ? [array_remove_null($item)]
                : [$key => array_remove_null($item)];
        })
        ->toArray();
}

$newArray = array_remove_null($array);

Hope this helps!




回答2:


In collection, use filter

some_collection->filter(function($value, $key) {
    return  $value != null;
});



回答3:


Give it a try:

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



回答4:


Try this:

   function array_filter_recursive($input) 
   { 
      foreach ($input as &$value) 
      { 
           if (is_array($value)) 
           { 
               $value = array_filter_recursive($value); 
           } 
      }     
      return array_filter($input, function($var){return !is_null($var);} ); 
   } 


来源:https://stackoverflow.com/questions/42829169/laravel-remove-elements-having-null-value-from-multidimentional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!