Recursion

Have you ever heard someone use this phrase: it’s a recursive function

well lets explain it quick:

Recursion:

A function that calls itself

So have you ever seen code that has many layers/levels of foreach loops do something?

I can actually recall a function that would be called and had around 4-5 foreach loops running and at each level the foreach loop would run a seperate SQL query and then foreach loop of that. It was quite a crazy pot of crazy. It worked none the less but for anyone else coming in, it could take quite a while to pick up the rhythm.

Let me get an example of a recursive function, its a bit of a classic.

function factorial($number) {
    if ($number < 2) {
        return 1;
    } else {
        print "factorial($number) is ($number * factorial(".($number-1)."))\n
"; return ($number * factorial($number-1)); } } print factorial(6);

when you run this you get this output

factorial(6) is (6 * factorial(5)) 
factorial(5) is (5 * factorial(4)) 
factorial(4) is (4 * factorial(3)) 
factorial(3) is (3 * factorial(2)) 
factorial(2) is (2 * factorial(1)) 
720

As you can see the function calls itself. So following this logic we can move on.
In the following I needed to pull out an entire tree of array results from the top level of pages. The tree could go down for however long it needed to.
So I have used only one foreach loop to loop through my results from the initial results and then build and add to the array if their is another level of pages below that page.

$pages = $this->model->fetch_records("SELECT * FROM pages WHERE dc_active = 'y' AND dc_parent_id = 0");
function grabChild(&$array, $level){	
	foreach($array as $k => $v){
		$query = $ci->db->query("SELECT page_name, page_id, page_parent_id FROM page WHERE page_parent_id = '".$v['dc_id']."'");
		if($query->result()){
			$innerResult = $query->result_array();
			$array[$k]['level'] = $level;
			$array[$k]['children'] = $innerResult;
			grabChild($array[$k]['children'], $level+1);
		}else {
			$array[$k]['level'] = $level;
		}
	}
	return $array;
}
$pages_array = grabChild($pages, 0);
print_r($pages_array);

What would this output?
well let me show you:

Array
(
    [0] => Array
        (
            [dc_id] => 4
            [dc_parent_id] => 0
            [dc_name] => Financial
            [level] => 0
        )

   [1] => Array
        (
            [dc_id] => 11
            [dc_parent_id] => 0
            [dc_name] => Parent 1
            [level] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [dc_id] => 12
                            [dc_parent_id] => 11
                            [dc_name] => Child 1
                            [level] => 1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [dc_id] => 13
                                            [dc_parent_id] => 12
                                            [dc_name] => child 2
                                            [level] => 2
                                        )

                                )

                        )

                )

        )

)

As you can see I have also put in a level check to see how far down we are. This is a nice function that helped create a site wide site map and also a navigation.
Hopefully this has given you some help and you have enjoyed reading.
Anything you agree/disagree please comment.




Random Posts