如何在多维数组上递归使用 array_filter() 函数以删除包含空值的键值对?
How to use array_filter() function recursively on a multi-dimensional array in order to remove the key-value pairs that contains null values?
有一个名为 $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 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);
}
}
}
执行上述代码后,我得到了与之前相同的数组。
如果有人能指导我实现这个输出数组,那对我真的很有帮助。
这方面请帮帮我。
提前致谢。
遗憾的是,没有允许您递归遍历数组和取消设置键的内置函数。最接近的是 array_walk_recursive
。然而,这是不够的,因为数组作为引用传入,并且所有变量赋值都在可调用函数中完成。这意味着虽然您可以改变元素,但实际上无法取消设置,除非添加膨胀代码。
相反,我们可以使用从 array_walk_recursive
派生的函数来执行密钥取消设置。 callabe
return 的函数是一个布尔值 true/false
指示是否删除元素。我们简单地执行我们的比较和 return true,否则 return 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 检查和 return我们的 true/false。
function unset_null_children($value, $key){
return $value === NULL;
}
希望这就是您要找的。
有一个名为 $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 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);
}
}
}
执行上述代码后,我得到了与之前相同的数组。
如果有人能指导我实现这个输出数组,那对我真的很有帮助。
这方面请帮帮我。
提前致谢。
遗憾的是,没有允许您递归遍历数组和取消设置键的内置函数。最接近的是 array_walk_recursive
。然而,这是不够的,因为数组作为引用传入,并且所有变量赋值都在可调用函数中完成。这意味着虽然您可以改变元素,但实际上无法取消设置,除非添加膨胀代码。
相反,我们可以使用从 array_walk_recursive
派生的函数来执行密钥取消设置。 callabe
return 的函数是一个布尔值 true/false
指示是否删除元素。我们简单地执行我们的比较和 return true,否则 return 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 检查和 return我们的 true/false。
function unset_null_children($value, $key){
return $value === NULL;
}
希望这就是您要找的。