如何在多维数组上递归使用 array_filter() 函数以删除包含空值的键值对? [重复]

问题描述 投票:0回答:1

有一个名为

$post_data
的关联数组。实际的数组非常大。为了供您参考,我只是将其中的两个元素放在下面:

Array
    (
        [0] => Array
            (
                [feed_id] => 1331
                [app_id] => 0
                [privacy] => 0
                [privacy_comment] => 0
                [type_id] => user_status
                [user_id] => 244
                [parent_user_id] => 0
                [item_id] => 140
                [time_stamp] => 1424256681
                [feed_reference] => 0
                [parent_feed_id] => 0
                [parent_module_id] => 
                [time_update] => 1424256681
                [app_title] => 
                [profile_page_id] => 0
                [user_server_id] => 0
                [user_name] => profile-244
                [full_name] => Campusknot .
                [gender] => 0
                [user_image] => 2015/02/0f34dbb95ce18b17611220b78c87f9a6%s.png
                [is_invisible] => 0
                [user_group_id] => 7
                [language_id] => 
                [feed_time_stamp] => 1424256681
                [can_post_comment] => 1
                [feed_status] => feed with smily <img src="http://34.144.40.142/file/pic/emoticon/default/smile.png" alt="Smile" title="Smile" title="v_middle" />
                [feed_title] => 
                [feed_link] => http://34.144.40.142/profile-244/status-id_140/
                [total_comment] => 0
                [feed_total_like] => 0
                [feed_is_liked] => 
                [feed_icon] => http://34.144.40.142/theme/frontend/foxplus/style/default/image/misc/application_add.png
                [enable_like] => 1
                [comment_type_id] => user_status
                [like_type_id] => user_status
                [likes] => Array
                    (
                    )

                [marks] => Array
                    (
                    )

                [bShowEnterCommentBlock] => 
                [feed_month_year] => 02_2015
                [feed_like_phrase] => 
                [profile_image] => http://34.144.40.142/file/pic/user/2015/02/0f34dbb95ce18b17611220b78c87f9a6_50_square.png
                [feed_image] => 
                [user_group_name] => faculty
            )
    [1] => Array
            (
                [feed_id] => 1310
                [app_id] => 0
                [privacy] => 0
                [privacy_comment] => 0
                [type_id] => photo
                [user_id] => 244
                [parent_user_id] => 0
                [item_id] => 606
                [time_stamp] => 1424235811
                [feed_reference] => 0
                [parent_feed_id] => 0
                [parent_module_id] => 
                [time_update] => 1424235811
                [app_title] => 
                [profile_page_id] => 0
                [user_server_id] => 0
                [user_name] => profile-244
                [full_name] => Campusknot .
                [gender] => 0
                [user_image] => 2015/02/0f34dbb95ce18b17611220b78c87f9a6%s.png
                [is_invisible] => 0
                [user_group_id] => 7
                [language_id] => 
                [feed_time_stamp] => 1424235811
                [can_post_comment] => 1
                [feed_title] => 
                [feed_image] => http://34.144.40.142/file/pic/photo/2015/02/02ff1a23db112db834b8f41748242bcb_240.png
                [feed_status] => new_image
                [feed_link] => http://34.144.40.142/photo/606/after_login/userid_244/
                [total_comment] => 0
                [feed_total_like] => 1
                [feed_is_liked] => 910
                [feed_icon] => http://34.144.40.142/theme/frontend/foxplus/style/default/image/module/photo.png
                [enable_like] => 1
                [comment_type_id] => photo
                [like_type_id] => photo
                [custom_css] =>  js_photo_item_606  photo_holder_image
                [custom_rel] => 606
                [custom_js] => 
                [no_target_blank] => 1
                [custom_data_cache] => Array
                    (
                        [parent_user_id] => 0
                        [parent_profile_page_id] => 
                        [user_parent_server_id] => 
                        [parent_user_name] => 
                        [parent_full_name] => 
                        [parent_gender] => 
                        [parent_user_image] => 
                        [parent_is_invisible] => 
                        [parent_user_group_id] => 
                        [parent_language_id] => 
                        [photo_id] => 606
                        [album_id] => 
                        [view_id] => 0
                        [module_id] => 
                        [group_id] => 0
                        [type_id] => 1
                        [privacy] => 0
                        [privacy_comment] => 0
                        [title] => after_login
                        [user_id] => 244
                        [destination] => 2015/02/02ff1a23db112db834b8f41748242bcb%s.png
                        [server_id] => 0
                        [mature] => 0
                        [allow_comment] => 0
                        [allow_rate] => 0
                        [time_stamp] => 1424235811
                        [total_view] => 0
                        [total_comment] => 0
                        [total_download] => 0
                        [total_rating] => 0.00
                        [total_vote] => 0
                        [total_battle] => 0
                        [total_like] => 1
                        [total_dislike] => 0
                        [is_featured] => 0
                        [is_cover] => 0
                        [allow_download] => 0
                        [is_sponsor] => 0
                        [ordering] => 0
                        [is_profile_photo] => 0
                        [is_liked] => 910
                        [description] => new_image
                        [extra_photo_id] => 
                        [name] => 
                    )

                [feed_info] => posted a photo
                [likes] => Array
                    (
                        [244] => Array
                            (
                                [like_id] => 910
                                [type_id] => photo
                                [item_id] => 606
                                [user_id] => 244
                                [time_stamp] => 1424247486
                                [profile_page_id] => 0
                                [user_server_id] => 0
                                [user_name] => profile-244
                                [full_name] => Campusknot .
                                [gender] => 0
                                [user_image] => 2015/02/0f34dbb95ce18b17611220b78c87f9a6%s.png
                                [is_invisible] => 0
                                [user_group_id] => 7
                                [language_id] => 
                                [action_time_stamp] => 
                            )

                    )

                [marks] => Array
                    (
                    )

                [bShowEnterCommentBlock] => 1
                [feed_month_year] => 02_2015
                [feed_like_phrase] => You&nbsp;like this.
                [profile_image] => http://34.144.40.142/file/pic/user/2015/02/0f34dbb95ce18b17611220b78c87f9a6_50_square.png
                [user_group_name] => faculty
            )
    )

我想从上面的数组中删除包含 null(即什么都没有)的键值对(包括从内部数组中删除此类键值对,无论它们存在于何处)。

我不想从包含空值的各个键中取消设置空值,我想从数组

$post_data
中删除相应的键值对,结果我应该得到清理后的
$post_data
数组。

基本上,我不想遍历整个关联数组(即迭代所有存在的内部数组并检查其中的每个键值对是否为空值)。这将更加复杂和耗时。我想使用内置的 PHP 数组函数(如

array_filter()
或其他有用的函数)来完成这件事。我尝试使用
array_filter()
但它不适合我。我认为我在递归使用这个函数时犯了一些错误。我尝试的代码如下:

foreach($post_data as $key=>$value) {
    array_map('array_filter', $value);
    if(is_array($value)) {
        foreach($value as $k=>$v)
        array_map('array_filter', $v);
        //array_filter($v);

      if(is_array($v)){
        foreach($v as $ke=>$va)
          array_map('array_filter', $va);
          //array_filter($va);
      }
    }            
  } 

执行上述代码后,我得到了与之前相同的数组。

如果有人可以指导我实现这个输出数组,那对我来说真的很有帮助。

请在这方面帮助我。

提前致谢。

php arrays recursion multidimensional-array associative-array
1个回答
2
投票

不幸的是,没有内置函数允许您递归地遍历数组并取消设置键。最接近的是

array_walk_recursive
。然而,这还不够,因为数组作为引用传入,并且所有变量分配都是在可调用函数内完成的。这意味着虽然您可以改变元素,但如果不添加膨胀代码,您实际上无法取消它的设置。

相反,我们可以使用从

array_walk_recursive
派生的函数来执行密钥取消设置。函数
callabe
返回一个布尔值
true/false
指示是否删除该元素。我们只需执行比较并返回 true,否则返回 false。

function walk_recursive_remove (array $array, callable $callback) {
    foreach ($array as $k => $v) {
        if (is_array($v)) {
            $array[$k] = walk_recursive_remove($v, $callback);
        } else {
            if ($callback($v, $k)) {
                unset($array[$k]);
            }
        }
    }

    return $array;
}

然后我们只需创建自己的函数,该函数接受迭代元素的值及其键,然后执行 NULL 检查并返回 true/false。

function unset_null_children($value, $key){
    return $value === NULL;
}

希望这就是您正在寻找的。

© www.soinside.com 2019 - 2024. All rights reserved.