chawezul Posted May 22, 2006 Share Posted May 22, 2006 Ok, here's my problem, I want my function to return an array of numbers that are the prime factors of the input number. My code works. It's all good. Everything is great. Except, the array that is returned is only ONE item long, and it is the last item in the array. So my assumption is that everytime it calls to add to the array, it simply empties the array (or recreates it) and then adds the item. If you can help me that'd be great!Code:[code]$result = Array();function prime($n, $y) { if ($y == 1) return true; elseif (($n % $y) == 0) return false; else return prime($n, $y-1);}function findprimes($n, $p) { if (! (prime($p, $p-1))) return findprimes($n, $p+1); elseif ($n == $p) { $result[] = $n; return $result; } elseif (($n % $p) == 0) { $result[] = $p; return findprimes($n/$p, $p); } else return findprimes($n, $p+1);}function primefactors($n) { return findprimes($n, 2);}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10164-array-recreation/ Share on other sites More sharing options...
samshel Posted May 22, 2006 Share Posted May 22, 2006 Try putting this line in the function function findprimes($n, $p) {global $result;hth Quote Link to comment https://forums.phpfreaks.com/topic/10164-array-recreation/#findComment-37893 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.