Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I re-iterate without limits in php

I have a function that retrieve the details of family member and can count the total of family member to the fifth generation. Below is my code snipped

function familyTree($id)
    {
        $total = 0;
        foreach(family($id) as $child){
            $total++;
            foreach(family($child->id) as $grand_child){
                $total++;
                foreach(family($grand_child->id) as $great_grand_child){
                    $total++;
                    foreach(family($great_grand_child->id) as $great_great_grand_child){
                    $total++;
                    }
                }
            }
        }
    
        return $total;
    }

My challenge is, how do I calculate the total number of family under the parent or anyone to infinity generation?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

From the above comment by Giso Stallenberg, the correct answer should be the one below:

function familyTree($id)
{
    $family = family($id);

    $total = count($family);

    foreach(family($id) as $child){
        $total += familyTree($child->id);     
    }

    return $total;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading