Today I will share a snippet I’ve used in a project. Using that function, you can recursively remove key-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;
}