Re: Anyone who can help me making a loop of this?

From: Date: Fri, 26 Jan 2001 10:46:46 +0000
Subject: Re: Anyone who can help me making a loop of this?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-36767@lists.php.net to get a copy of this message
The "easiest" way to do that kind of output is "recursion" It's a kind of iteration/looping thing where a function calls itself. Sample: function fact($x){ if ($x == 0){ return 1; } else{ return $x * fact($x - 1); } } Basically the function eats itself rather like those Russion porcelain dolls nestled inside each other. It's easier to "reverse" your data structure to get it to work though: $tingtang[0] = array(1, 7); $tingtang[1] = array(2, 4); $tingtang[2] = array(3); . . . IE, each tingtang knows its "children", rather than the parent. In your case it would be something like: function tingtang($tt, $depth = 0){ global $tingtang; echo str_repeat("&nbsp;", $depth * 4), "tingtang$tt<BR>\n"; while (list(,$child) = each($tingtang[$tt])){ tingtang($child, $depth+1); } } tingtang(0); Note: Recursion has an overhead at each function call -- So very "deep" trees can become performance problems. For a much more in-depth look at this sort of thing, check out the rather long threads about "forums" and "recursion" in the PHP Mailing List archives. (Probably back in version 3 archives, actually) -- Visit the Zend Store at http://www.zend.com/store/ Wanna help me out? Like Music? Buy a CD: http://l-i-e.com/artists.htm Volunteer a little time: http://chatmusic.com/volunteer.htm ----- Original Message ----- From: Christian Sakshaug <christian@sakshaug.net> Newsgroups: php.general Sent: Thursday, January 25, 2001 12:34 PM Subject: [PHP] Anyone who can help me making a loop of this? > Anyone who can help me making a loop of this? > http://www.sakshaug.net/phps/loop.txt > > I have tried many things, but I can't get it perfect... > > > > > > Best Regards, Christian Sakshaug > -- > Email: christian@sakshaug.net > Web: www.sakshaug.net > Tel: +47 913 95 618 > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#36767) next »