php - MYSQL复制数组值

标签 php html mysql

    error_reporting(E_ALL);
echo "<pre>";

// DISTRIBUTE TEAMS INTO CONTESTS
$fixtures = mysqli_query($link, "SELECT teamname FROM tourn_teams WHERE groupname='Group 1'");

$teams = array();
// THE TEAMS
while($row = mysqli_fetch_assoc($fixtures))
{ 

  $teams[] = $row['teamname'];

}

// HOW MANY WEEKS
$weeks = 3;

// MAKE ENOUGH ARRAY ELEMENTS FOR THE DISTRIBUTION
$array = array_merge($teams, $teams);


// POPULATE THE MATCHES ARRAY
$matches = array();
while ($weeks)
{
    foreach ($teams as $ptr => $team)
    {
        // FIND THE INDEX INTO THE DISTRIBUTION ARRAY
        $linkt = $ptr + $weeks;

        // SELECT THE HOME AND AWAY TEAMS
        $home = $team;
        $away = $array[$linkt];
        $matches[$team][$weeks] = array('home' => $home, 'away' => $away);
    }

    // NEXT WEEK
    $weeks--;
}


// SORT THE MATCHES SENSIBLY SO WEEK ONE COMES FIRST
foreach ($matches as $team => $contests)
{
    ksort($contests);
    $matches[$team] = $contests;
}


// ACTIVATE THIS TO SEE WHAT THE $matches ARRAY LOOKS LIKE
// print_r($matches);


// CREATE THE TABLE OF MATCHUPS
$out = NULL;
$out .= "<table>";
$out .= PHP_EOL;


// CREATE THE HEADERS FOR EACH WEEK
$weeknums = end($matches);

$out .= "<tr>";
$out .= '<th> Team </th>';
$out .= '<th> v </th>';
$out .= "<th> Team </th>";
$out .= '</tr>';
$out .= PHP_EOL;


// CREATE THE MATRIX OF MATCHUPS
foreach ($matches as $team => $contests)
{
    $out .= "<form class='form-horizontal' action='".$_SERVER['PHP_SELF']."'d method='post'><tr><td><input type='text' name='teamone' value='$team' readonly></td>";
    $out .= "<td> <b>v</b></td>";
    foreach ($contests as $week => $matchup)
    {
print_r($matchup);
        $out .= "<td> <input type='text' name='teamtwo' value='{$matchup["away"]}' readonly> </td>";
    }
    $out .= "</tr>";
    $out .= PHP_EOL;
}
$out .= "<input class='btn btn-primary' type='submit' name='submit'></form></table>";
$out .= PHP_EOL;

foreach ($matches as $team => $contests)
{
foreach ($contests as $week => $matchup)
    {
    if(is_array($matchup)){
    foreach($matchup as $key => $value){
    $home = $matchup['home'];
    $away = $matchup[away];

    $sql = mysqli_query($link, "INSERT INTO tourn_fixtures(teamone, teamtwo) values ('$home', '$away')");
    }
}
}
}


echo "</pre>";
echo $out;

这是我的代码。当我尝试在//CREATE THE MATRIX OF MATCHUPS 之后运行 sql 查询时,它可以工作并执行我想要的操作,但它会将 2 个相同的行插入数据库。

Database

我一直在玩,但找不到复制它的内容。我只想要一行而不是两行相同的行。

而且我刚刚注意到,它稍后会生成两个相同的灯具...

Duplicate

这是我得到的数组的输出;

    Array
(
    [home] => Committee All-Stars
    [away] => Vets
)
Array
(
    [home] => Committee All-Stars
    [away] => Lightning
)
Array
(
    [home] => Committee All-Stars
    [away] => Bolt
)
Array
(
    [home] => Vets
    [away] => Lightning
)
Array
(
    [home] => Vets
    [away] => Bolt
)
Array
(
    [home] => Vets
    [away] => Firsts
)
Array
(
    [home] => Lightning
    [away] => Bolt
)
Array
(
    [home] => Lightning
    [away] => Firsts
)
Array
(
    [home] => Lightning
    [away] => Committee All-Stars
)
Array
(
    [home] => Bolt
    [away] => Firsts
)
Array
(
    [home] => Bolt
    [away] => Committee All-Stars
)
Array
(
    [home] => Bolt
    [away] => Vets
)
Array
(
    [home] => Firsts
    [away] => Committee All-Stars
)
Array
(
    [home] => Firsts
    [away] => Vets
)
Array
(
    [home] => Firsts
    [away] => Lightning
)

我只希望每个团队互相比赛一次。

最佳答案

你的问题出在你的最后一个foreach上。 $matchup 是一个类似的数组

Array
(
    [home] => All-Stars
    [away] => Vets
)

并且您正在迭代其值,因此您将为每个匹配执行两次插入。您实际上并不需要 foreach ,只需使用

foreach ($contests as $week => $matchup) {
    $home = $matchup['home'];
    $away = $matchup['away'];
    $sql = mysqli_query($link, "INSERT INTO tourn_fixtures(teamone, teamtwo) values ('$home', '$away')");
}

请注意,您还需要将 away 括起来作为 $matchup 的索引。

以下是代码演示,显示为每个 $matchup 生成两个 INSERT 查询:Demo on 3v4l.org

关于php - MYSQL复制数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59556976/

相关文章:

php - 附加没有默认 "next line"填充的 img

html - 如何使白色div中的TEXT完全透明以显示背景?

php - PHP UPDATE将使用int而不是采用表单输入的数据进行更新

php - 如何使用格式为 [col1=>[col2=>col3,col2=>col3]] 的 PDO 获取数据?

php - 如何每1分钟检查一次,然后将其发送给客户端?

PHP 数组 - 用值更改键名

jQuery:向上或向下滚动时隐藏一个div

javascript - 单击按钮后如何将文本颜色更改为 gif(背景剪辑)?

mysql - 删除一行后重新调整 auto_increment 主键字段

mysql - 未使用配置单元 MySQL Metastore 中的 Spark 构建