PHP

PHP to remove unnecessary key and value pairs from any multi-dimensional array

Today I will share a snippet I’ve used in a project. Using that function, you can recursively remove unnecessary key and value pairs from any multi-dimensional array. Here is the function :

 

function deep_clean($array) {
    $remove_keys = [
  'key_1',
  'key_2',
  'key_3'
    ];

    $array = array_diff_key($array, array_flip($remove_keys));

    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $array[$key] = deep_clean($value);
        }
    }

    return $array;
}
S M Zahed Kamal

Recent Posts

Open the side panel by default in google chrome extension

To open the side panel by default in google Chrome extension, you can use the…

10 months ago

Use vanilla JavaScript to make Ajax request

JavaScript AJAX (Asynchronous JavaScript and XML) is a technique that gives the ability to send…

4 years ago

Add animation to bootstrap carousel elements

By default, Bootstrap carousel has no way to add animations to carousel elements. Here I'm…

4 years ago

Create custom pagination template in Laravel

Laravel comes up with a paginator that generates HTML compatible with the Tailwind CSS framework.…

4 years ago

Add Bootstrap Icons in SASS or SCSS

Bootstrap introduced their icons collection in November 2019 as Bootstrap Icons (v1.0.0-alpha). At that time,…

4 years ago

Create autocomplete using vanilla JavaScript

To create autocomplete feature for input field(s), HTML's datalist tag can be the easiest solution.…

4 years ago