Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 混淆foreach循环-访问数组中的值,然后将其重新存储到变量中_Php_Arrays_Multidimensional Array_Foreach - Fatal编程技术网

Php 混淆foreach循环-访问数组中的值,然后将其重新存储到变量中

Php 混淆foreach循环-访问数组中的值,然后将其重新存储到变量中,php,arrays,multidimensional-array,foreach,Php,Arrays,Multidimensional Array,Foreach,我有以下多维数组: Array ( [0] => Array ( [0] => Array ( [name] => team0 [games] => Array ( [0] => 3

我有以下多维数组:

   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
                        )

                )

        )
我试图遍历数组,并将数组中的信息存储在各个变量中,以便使用这些变量作为参数对数据库执行查询。我发现使用这个多维数组非常令人困惑,我很难理解如何正确地迭代数组。事实上,这是我的代码。。。在这个阶段,我更多的是干涉foreach循环,试图更好地理解如何实现我需要做的事情。这段代码存在一个问题,在每个团队名称“Array”也被呼出之后

foreach ($arrTeam as $array) 
            foreach ($array as $groupid => $group){
                $in_groupID = $groupid+1;
                foreach ($group as $name) {
                    if (isset($name)) echo $name.'<br>'; 
                }                   
            }
foreach($arrteamas$array)
foreach($groupid=>$group的数组){
$in_groupID=$groupID+1;
foreach($group as$name){
if(isset($name))回显$name。“
”; } }
基本上,我想做的是从数组中获取一个groupid和teamname,将它们存储在
$in_groupid
$in_teamname
中,然后深入数组,一次抓取一个特定团队的每一场比赛-对我抓取的每一场比赛在数据库上执行查询。查询本身不是问题,它以我想要的方式在这个混乱的循环中迭代,并将值存储为执行的参数

任何帮助都将不胜感激。正如我所说,我不关心执行数据库查询之类的事情,我可以很容易地修复它并在以后添加它,我只需要集中精力存储循环中的参数以供执行


非常感谢您的帮助。

当您遍历团队阵列时,您将能够访问
名称
游戏
阵列。您不需要遍历每个团队,因为您知道每个团队的关键。您需要迭代每个团队的所有游戏:

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
    }
}

斯蒂芬,接下来呢

   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

        }

什么是
groupid
?第一个数组的索引?是的,尽管我刚刚意识到这是我犯的一个小错误。这实际上应该是“teamid”,但对于代码片段来说,名称并不重要,我想,第一个索引是GroupID,第二个是teamid。我已经修复了我原来的帖子。多么可怕的错误!抱歉造成混淆。我收到了此代码的未定义索引通知,以及:警告:为以下行中的foreach()提供的参数无效:foreach($team['games']as$number){调用
print\r($team)时会发生什么情况
在第一个foreach中?请参考我原始问题中的新数组结构,我已经对它进行了更新。这是一个多么可怕的疏忽,我实际上粘贴了错误的print_r()转储,并且没有注意到。