|
Posted by Vince Morgan on 04/12/07 13:38
"Rienk Withaar" <r.g.withaar@rug.nl> wrote in message
news:evl3vj$abq$2@info.service.rug.nl...
> Hello, I have the following problem:
>
> I want my function to return a value, $l, using the following function:
>
> function level($n,$l=0,$d=false) {
> if ($d) { return $l; }
> else {
> $ff=get_folders();
> for ($i=0;$i<count($ff);$i++) {
> if ($n==$ff[$i][0]) {
> if ($ff[$i][4]!=0) {
> $l++;
> level($ff[$i][4],$l,$d);
> }
> else {
> $d=true;
> level($ff[$i][0],$l,$d);
> }
> }
> }
> }
> }
>
function level($n, $1=0,$d=false){
$ff=get_folders();
$c=count($ff);
for($i=0;$i<$c){
if($n==$ff[$i][0]){
if($ff[$i][4]!=0){
level($ff[$i][0],$1,$d);
}
}
else{
return $1;
}
}
If($1 = "some upper limit"){
die('level() has recursed '.$1.' times without finding the folder')
}
}
Please excuse me taking the liberty of altering your function slightly. In
doing so it became apparent that there is a very real chance that this could
cause a stack overflow if there is no check for a complete failure to find
the folder, so I added one.
get_folders() is always going to return the same array on every recursion,
unless you make another one in an astoundingly short time, and should be
called from outside this function.
It's past my bed time so there could be some horrible errors in the above.
Vince
Navigation:
[Reply to this message]
|