RyanTuosto Posted May 21, 2009 Share Posted May 21, 2009 I'm trying to 1. Loop through each owner 2. Loop through the current owner's dogs 3. Get a random list of the current owner's dogfood 4. Feed the current dog 3 times 5. Move on to the next dog of that owner. Foreach ($owners as $owner) { For($x=1;$x<=$NumOfDogs;$x++) { $dog = $dogs[$x]; shuffle($dogFoods); Foreach ($dogFoods as $dogFood) { For($y=1;$y<3;$y++) { Feed($dog, $dogFood); } } } } What am I missing? Link to comment https://forums.phpfreaks.com/topic/159012-nested-loop-help/ Share on other sites More sharing options...
Andy-H Posted May 21, 2009 Share Posted May 21, 2009 Foreach ($owners as $owner) { For($x=0; $x< count($dogs) - 1; $x++) { // NUMERICALLY INDEXED ARRAYS START AT 0 $dog = $dogs[$x]; shuffle($dogFoods); Foreach ($dogFoods as $dogFood) { For($y=1; $y<3; $y++) { Feed($dog, $dogFood); } } } } Link to comment https://forums.phpfreaks.com/topic/159012-nested-loop-help/#findComment-838599 Share on other sites More sharing options...
Andy-H Posted May 21, 2009 Share Posted May 21, 2009 Also, I think you are feeding each dog 3 times with each type of dogfood. Try: Foreach ($owners as $owner) { For($x=0; $x< count($dogs) - 1; $x++) { // NUMERICALLY INDEXED ARRAYS START AT 0 $dog = $dogs[$x]; For($y=1; $y<3; $y++) { shuffle($dogFoods); $dogFood = $dogFoods[0]; Feed($dog, $dogFood); } } } Link to comment https://forums.phpfreaks.com/topic/159012-nested-loop-help/#findComment-838600 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.