php Laravel -从多维数组中删除具有NULL值的元素

slmsl1lt  于 2022-12-17  发布在  PHP
关注(0)|答案(4)|浏览(259)

我使用的是Laravel 5.3
我有一个multidimensional array,如下所示:

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 */
                                        )
                                )
                        )
                )
        )
)

我想把remove乘以elements,得到NULL,结果应该是:

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
                                        (
                                        )
                                )
                        )
                )
        )
)

如何做到这一点?

bprjcwpo

bprjcwpo1#

在集合中,使用筛选器

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

pbossiut2#

如果你想删除null值而不是空数组,你可以这样做:

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);

希望这有帮助!

0md85ypi

0md85ypi3#

有两个选项可供用途:

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

$yourArr = array_filter( $yourArr);

将删除所有具有空值的键

dtcbnfnu

dtcbnfnu4#

试试这个:

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);} ); 
   }

相关问题