getting wrong results from foreach

I have following code:

$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();
            }
        }

This gives me the following database elements:

在这里输入图像描述

The problem is that I keep getting Soccerteam18 and FC Beer, these should be all different teams. What is wrong with my foreach-loop?

I fill $teamsA like this:

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

上一篇: Foreach循环取决于目录中的文件

下一篇: 从foreach得到错误的结果