How do I recursively delete a directory and its entire contents (files + sub dirs) in PHP? [duplicate]

The user-contributed section in the manual page of rmdir contains a decent implementation: function rrmdir($dir) { if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects as $object) { if ($object != “.” && $object != “..”) { if (is_dir($dir. DIRECTORY_SEPARATOR .$object) && !is_link($dir.”https://stackoverflow.com/”.$object)) rrmdir($dir. DIRECTORY_SEPARATOR .$object); else unlink($dir. DIRECTORY_SEPARATOR .$object); } } rmdir($dir); } }

Java recursive Fibonacci sequence

In fibonacci sequence each item is the sum of the previous two. So, you wrote a recursive algorithm. So, fibonacci(5) = fibonacci(4) + fibonacci(3) fibonacci(3) = fibonacci(2) + fibonacci(1) fibonacci(4) = fibonacci(3) + fibonacci(2) fibonacci(2) = fibonacci(1) + fibonacci(0) Now you already know fibonacci(1)==1 and fibonacci(0) == 0. So, you can subsequently calculate the other … Read more

How to search by key=>value in a multidimensional array in PHP

Code: function search($array, $key, $value) { $results = array(); if (is_array($array)) { if (isset($array[$key]) && $array[$key] == $value) { $results[] = $array; } foreach ($array as $subarray) { $results = array_merge($results, search($subarray, $key, $value)); } } return $results; } $arr = array(0 => array(id=>1,name=>”cat 1″), 1 => array(id=>2,name=>”cat 2″), 2 => array(id=>3,name=>”cat 1″)); print_r(search($arr, ‘name’, … Read more

Recursion in Angular directives

Inspired by the solutions described in the thread mentioned by @dnc253, I abstracted the recursion functionality into a service. module.factory(‘RecursionHelper’, [‘$compile’, function($compile){ return { /** * Manually compiles the element, fixing the recursion loop. * @param element * @param https://stackoverflow.com/questions/14430655/recursion-in-angular-directives A post-link function, or an object with function(s) registered via pre and post properties. * … Read more

Nested defaultdict of defaultdict

The other answers here tell you how to create a defaultdict which contains “infinitely many” defaultdict, but they fail to address what I think may have been your initial need which was to simply have a two-depth defaultdict. You may have been looking for: defaultdict(lambda: defaultdict(dict)) The reasons why you might prefer this construct are: … Read more

Can every recursion be converted into iteration?

Can you always turn a recursive function into an iterative one? Yes, absolutely, and the Church-Turing thesis proves it if memory serves. In lay terms, it states that what is computable by recursive functions is computable by an iterative model (such as the Turing machine) and vice versa. The thesis does not tell you precisely … Read more