从foreach得到错误的结果

我有以下代码:

$arraySlice1 = array_slice($teamsA, 0, 8);
        $arraySlice2 = array_slice($teamsA, 8, 15);

        foreach ($arraySlice1 as $teamHome) {
            $dateCounter = 1;
            foreach ($arraySlice2 as $teamAway) {
                    $tactic1 = $doctrine->getRepository('LoginLoginBundle:Tactics')
                            ->findByteamTeamid($teamHome->getTeamid());
                    $tactic2 = $doctrine->getRepository('LoginLoginBundle:Tactics')
                            ->findByteamTeamid($teamAway->getTeamid());
                    $match = new Matchgame();
                    $match->setAwayteam($teamAway->getName());
                    $match->setHometeam($teamHome->getName());
                    $match->setIsplayed(false);
                    $match->setSeasonSeasonid($season);
                    $date = new DateTime(date('Y-m-d'));
                    $date->add(new DateInterval('P' . $dateCounter . 'D'));
                    $match->setDate($date);
                    $dateCounter+=2;
                    $tacticMatchHome = $tactic1[0];
                    $tacticMatchHome->setMatchMatchid($match);
                    $tacticMatchAway = $tactic2[0];
                    $tacticMatchAway->setMatchMatchid($match);
                    $em->persist($match);
                    $em->flush();
                    $em->persist($tacticMatchHome);
                    $em->flush();
                    $em->persist($tacticMatchAway);
                    $em->flush();
            }
        }

        foreach ($arraySlice2 as $teamHome) {
            $dateCounter = 2;
            foreach ($arraySlice1 as $teamAway) {
                    $tactic1 = $doctrine->getRepository('LoginLoginBundle:Tactics')
                            ->findByteamTeamid($teamHome->getTeamid());
                    $tactic2 = $doctrine->getRepository('LoginLoginBundle:Tactics')
                            ->findByteamTeamid($teamAway->getTeamid());
                    $match = new Matchgame();
                    $match->setAwayteam($teamAway->getName());
                    $match->setHometeam($teamHome->getName());
                    $match->setIsplayed(false);
                    $match->setSeasonSeasonid($season);
                    $date = new DateTime(date('Y-m-d'));
                    $date->add(new DateInterval('P' . $dateCounter . 'D'));
                    $match->setDate($date);
                    $dateCounter+=2;
                    $tacticMatchHome = $tactic1[0];
                    $tacticMatchHome->setMatchMatchid($match);
                    $tacticMatchAway = $tactic2[0];
                    $tacticMatchAway->setMatchMatchid($match);
                    $em->persist($match);
                    $em->flush();
                    $em->persist($tacticMatchHome);
                    $em->flush();
                    $em->persist($tacticMatchAway);
                    $em->flush();
            }
        }

这给了我以下数据库元素:

在这里输入图像描述

问题是我一直拿Soccerteam18和FC啤酒,这些应该是所有不同的球队。 我的foreach循环出了什么问题?

我这样填充$ teamsA:

$teamsA = $doctrine->getRepository('LoginLoginBundle:Team')
                ->findBydivisionDivisionid($divisionA[0]->getDivisionid());
链接地址: http://www.djcxy.com/p/47807.html

上一篇: getting wrong results from foreach

下一篇: Adjusting last iteration in a foreach loop in PHP