FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » how to make a function recursive
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: how to make a function recursive [message #174970 is a reply to message #174968] Wed, 27 July 2011 19:51 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On Wed, 27 Jul 2011 10:58:55 -0700, jr wrote:

The following will, I think, do what you want. As an exercise in learning
php, I leave it to you to work out how it does it. Sorry if these seems
rather abrupt of me, but your question sounds like a tutorial or homework
of some sort, so I'm not prepared to provide both an explanation and
working code.

<?php
function t($a)
{
$c = 0;
foreach ($a as $i=>$d) if ($d>0) $c++;
return $c;
}
function r($a,$i=0)
{
if (t($a)==1) return $a;
do
{
$i+=1;
if ($i==count($a)) $i=0;
}
while (($a[$i])<0);
$a[$i]=-1;
do
{
$i+=1;
if ($i==count($a)) $i=0;
}
while (($a[$i])<0);
return r($a,$i);
}
$a = array();
for ($i=0;$i<100;$i++) $a[]=1;
$a = r($a);
foreach ($a as $i=>$d) if ($d>0) echo "{$i} ";
echo "\n";
?>

Rgds

Denis McMahon
[Message index]
 
Read Message
Read Message
Read Message
Previous Topic: Getting error while requesting data from the UPS webservice
Next Topic: Avoiding Proxy Caching
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 03:33:24 GMT 2024

Total time taken to generate the page: 0.03175 seconds