0

There is one associative array titled $post_data. The actual array is very large. For your reference I'm just putting below two elements from it :

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

I want to remove the key-value pairs from the above array that contains null(i.e. nothing) (including removal of such key-value pairs from the inner arrays wherever they are present).

I don't want to unset the null values from the respective keys containing null value, I want to delete that respective pair of key-value from the array $post_data and in result I should get the cleaned up $post_data array.

Basically, I don't want to traverse through the whole associative array(i.e. iterating through all the inner arrays present and check each key-value pair in it for null value). It will be more complicated and time-consuming. I want to get this thing done using built-in PHP array functions like array_filter() or something else useful. I tried using array_filter() but it didn't work out for me. I think I'm making some mistake in using this function recursively. The code I tried is as follows:

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

After execution of the above code I'm getting the same previous array.

If someone could guide me in achieving this output array it would be really helpful to me.

Please help me out in this regard.

Thanks in advance.

0

1 Answer 1

2

Unfortunately there's no built in function that allows you to walk an array recursively and unset keys. The closest is array_walk_recursive. This falls short, however, as the array is passed in as a reference, and all variable assignment is done within the callable function. This means that while you can mutate the element, you can't actually unset it, without adding bloat code.

Instead, we can use a function that was forked from array_walk_recursive that performs key unsetting. The function that is callabe returns a boolean true/false indicating whether or not to remove the element. We simply perform our comparison and return true, otherwise 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;
}

Then we just create our own function that accepts the value of the iterated element and it's key, then performs the NULL check and returns our true/false.

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

Hopefully this is what you're looking for.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.