匹配二维数组



伙计们,我正在尝试匹配二维数组。下面的代码演示了我正在做的事情:

$games = array("Game1" => "Team 2 vs. Team 3", "Game2" => "Team 1 vs. Win 1","Game3" => "Loser Game 1 vs. Loser Game 2","Game4" => "Win Game 2 vs. Win Game 3", "Game5" => "Win Game 4 vs. Loser Game 4 (If Loser 4 is First Loss)");
$time= array("06:00 " => "06:30","06:30 " => " 07:00","07:00" => "07:30","07:30" =>"08:00","08:00" => "08:30");
foreach($games as $games => $versus)
{
   foreach($time as $start_time => $end_time)
   {
      echo $games . ": ". $versus ." ". $start_time. " to " . $end_time . "<br>";
   }
}

但是,这输出

Game1: Team 2 vs. Team 3 = 06:00 to 06:30
Game1: Team 2 vs. Team 3 = 06:30 to 07:00
Game1: Team 2 vs. Team 3 = 07:00 to 07:30
Game1: Team 2 vs. Team 3 = 07:30 to 08:00
Game1: Team 2 vs. Team 3 = 08:00 to 08:30
Game2: Team 1 vs. Win 1 = 06:00 to 06:30
Game2: Team 1 vs. Win 1 = 06:30 to 07:00
Game2: Team 1 vs. Win 1 = 07:00 to 07:30
Game2: Team 1 vs. Win 1 = 07:30 to 08:00
Game2: Team 1 vs. Win 1 = 08:00 to 08:30
Game3: Loser Game 1 vs. Loser Game 2 = 06:00 to 06:30
Game3: Loser Game 1 vs. Loser Game 2 = 06:30 to 07:00
Game3: Loser Game 1 vs. Loser Game 2 = 07:00 to 07:30
Game3: Loser Game 1 vs. Loser Game 2 = 07:30 to 08:00
Game3: Loser Game 1 vs. Loser Game 2 = 08:00 to 08:30
Game4: Win Game 2 vs. Win Game 3 = 06:00 to 06:30
Game4: Win Game 2 vs. Win Game 3 = 06:30 to 07:00
Game4: Win Game 2 vs. Win Game 3 = 07:00 to 07:30
Game4: Win Game 2 vs. Win Game 3 = 07:30 to 08:00
Game4: Win Game 2 vs. Win Game 3 = 08:00 to 08:30
Game5: Win Game 4 vs. Loser Game 4 (If Loser 4 is First Loss) = 06:00 to 06:30
Game5: Win Game 4 vs. Loser Game 4 (If Loser 4 is First Loss) = 06:30 to 07:00
Game5: Win Game 4 vs. Loser Game 4 (If Loser 4 is First Loss) = 07:00 to 07:30
Game5: Win Game 4 vs. Loser Game 4 (If Loser 4 is First Loss) = 07:30 to 08:00
Game5: Win Game 4 vs. Loser Game 4 (If Loser 4 is First Loss) = 08:00 to 08:30

我想这样显示

Game1: Team 2 vs. Team 3 06:00 to 06:30
Game2: Team 1 vs. Win 1 06:30 to 07:00
Game3: Loser Game 1 vs. Loser Game 2 07:00 to 07:30
Game4: Win Game 2 vs. Win Game 3 = 07:30 to 08:00
Game5: Win Game 4 vs. Loser Game 4 (If Loser 4 is First Loss) = 08:00 to 08:30

我该如何解决这个问题?

如果您可以控制数组,我会使用不同的结构,这样您可以轻松匹配游戏和时间。

但是,由于您没有数字键,您需要从$time数组中获取键key()和值current(),并在每次循环迭代时移动数组指针:

foreach($games as $game => $versus)
{
    echo $game . ": " . $versus ." ". key($time) ." to ". current($time) ."<br>";
    next($time);
}

实际上使用each():

可能更简单
foreach($games as $game => $versus)
{
    list($start, $end) = each($time);
    echo $game . ": " . $versus ." ". $start ." to ". $end ."<br>";
}

如果可能的话,你可以尝试将游戏和开始/结束时间组合成一个多维数组(这只是许多可能的方法之一)。这个例子是2个游戏,但你可以添加更多:

$games = array(
    "Game1" => 
            Array(
                "versus" => "Team 2 vs. Team 3",
                "start_time" => "06:00",
                "end_time" => "06:30"
            ),
     "Game2" => 
            Array(
                "versus" => "Team 1 vs. Win 1",
                "start_time" => "06:30",
                "end_time" => "07:00"
            )
);
foreach ($games as $game => $arr)
{
    echo "<br>";
    echo $game . ": versus : " . $arr['versus'] . ": start_time: ";
    echo $arr['start_time'] . ": end_time: " . $arr['end_time'];
}
输出:

Game1: vs: Team 2 vs. Team 3: start_time: 06:00: end_time: 06:30

Game2: vs: Team 1 vs. Win 1: start_time: 06:30: end_time: 07:00

相关内容

  • 没有找到相关文章

最新更新