anditpainsme
anditpainsme

Reputation: 649

Confusing foreach loop - accessing and then re-storing values into variables from array

I have the following multidimensional array:

   Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [name] => team0
                    [games] => Array
                        (
                            [0] => 3
                        )

                )

            [1] => Array
                (
                    [name] => team1
                    [games] => Array
                        (
                            [0] => 2
                        )

                )

            [2] => Array
                (
                    [name] => team2
                    [games] => Array
                        (
                            [0] => 1
                        )

                )

            [3] => Array
                (
                    [name] => team3
                    [games] => Array
                        (
                            [0] => 0
                        )

                )

        )

I am trying to traverse through the array and store information from the array in individual variables to execute a queries on a database using these variables as parameters. I am finding working with this multidimensional array very confusing and I am struggling to understand how to properly iterate over the array. As it is, this is my code... which at this stage is more just me meddling with foreach loops, trying to grasp a better understanding of how I can achieve what I need to do. There is a problem with this code as it is, after every team name "Array" is also echoed out.

foreach ($arrTeam as $array) 
            foreach ($array as $groupid => $group){
                $in_groupID = $groupid+1;
                foreach ($group as $name) {
                    if (isset($name)) echo $name.'<br>'; 
                }                   
            }

Basically what I want to do is fetch a groupid and teamname from the array, store them in $in_groupID and $in_teamName, and then traverse deeper into the array and grab each game for that specific team one game at a time - executing the query on the database for each game I grab. The query itself is not a problem, it's iterating through this confusing loop in the way that I want to and storing the values as parameters for the execution.

Any help would be majorly appreciated. As I said, I'm not concerned with executing the database query and whatnot, I can easily fix that and add it in later, I just need to get my head around storing the parameters from the loop for the execute.

Thanks a lot for any help.

Upvotes: 0

Views: 457

Answers (2)

Paul T. Rawkeen
Paul T. Rawkeen

Reputation: 4114

Stephen, and what about next ?

   foreach ($someInitArr as $rootIndex => $arrTeam)
      foreach ($arrTeam as $teamid => $team) {

        $teamName = $team['name'];
        $teamID = $teamid + 1;

            foreach($team['games'] as $gameNumber => $gameValue)
              // here yo can grab each game separately within the database already having $teamName and $teamID

        }

Upvotes: 1

Bailey Parker
Bailey Parker

Reputation: 15905

When you iterate through your team array, you'll be able to access the name and games arrays. You don't need to iterate through each team, because you know the keys of each team. What you need to iterate through is all of the games for each team:

foreach($teams as $teamId => $team) {
    // team id is in $teamId
    $teamName = $team['name'];

    // Iterate through the games
    foreach($team['games'] as $number) {
        // The game number (or id, or whatever you call it) is in $number
    }
}

Upvotes: 1

Related Questions