Jump to content

Nested Loop Help!


RyanTuosto

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.