severndigital Posted October 4, 2008 Share Posted October 4, 2008 i have this recursive function within a class protected function checkArray() { $item = rand(0,parent::makeGrid()); if(in_array($item,$this->_locations)){ $this->checkArray(); }else{ return $item; } } //function that reference the above function public function hideTreasure(){ //echo $this->_totTreas; for($i = 0; $i < $this->_totTreas; $i++) { $this->_locations[] = $this->checkArray(); } return $this->_locations; } i designed it as a recusive function that basically it generates a random number within a rand and checks it against an array. when i run this function it works fine, however if ask it to generate more than a few random numbers it starts to generate blanks. here is the array from 25 randoms Array ( [0] => 95 [1] => 22 [2] => 16 [3] => 9 [4] => 5 [5] => 51 [6] => 82 [7] => 53 [8] => 58 [9] => 72 [10] => 14 [11] => 68 [12] => 83 [13] => 74 [14] => 1 [15] => 57 [16] => 44 [17] => 90 [18] => 39 [19] => 79 [20] => 54 [21] => 6 [22] => 36 [23] => [24] => 45 ) Any ideas why it would be doing that?? Thanks, C Quote Link to comment https://forums.phpfreaks.com/topic/126999-odd-function-problem-skipping-entries/ Share on other sites More sharing options...
severndigital Posted October 6, 2008 Author Share Posted October 6, 2008 bump Quote Link to comment https://forums.phpfreaks.com/topic/126999-odd-function-problem-skipping-entries/#findComment-658069 Share on other sites More sharing options...
Brian W Posted October 6, 2008 Share Posted October 6, 2008 You have empty spaces in your array? idk man, I don't understand your bit of code very well. But I would look at your array very closely for problems there. Quote Link to comment https://forums.phpfreaks.com/topic/126999-odd-function-problem-skipping-entries/#findComment-658113 Share on other sites More sharing options...
wildteen88 Posted October 6, 2008 Share Posted October 6, 2008 I think this line: if(in_array($item,$this->_locations)){ $this->checkArray(); } Should be: if(in_array($item,$this->_locations)){ return $this->checkArray(); } Quote Link to comment https://forums.phpfreaks.com/topic/126999-odd-function-problem-skipping-entries/#findComment-658230 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.