使用带有键的数组作为数组键

Using an array with a key as an array key

我在下面的代码中使用带有键的数组作为数组的键,

        $team = array();
        $counter = 0;

        $sql = "SELECT Home, Away, Result, Points FROM schedule";
        $schedulequery = mysqli_query($conn, $sql);

        // divy out the points
        if (mysqli_num_rows($schedulequery) > 0) {
            while($teamrow = mysqli_fetch_assoc($schedulequery)) {

                $points = $teamrow["Points"]; 
                if ($teamrow["Result"] == "Home"){
                    $team[$teamrow["Home"]] = $team[$teamrow["Home"]] + $points;
                }
                if ($teamrow["Result"] == "Tie"){
                    $team[$teamrow["Home"]] = $team[$teamrow["Home"]] + ($points/2);
                    $team[$teamrow["Away"]] = $team[$teamrow["Away"]] + ($points/2);
                }
                if ($teamrow["Result"] == "Away"){
                    $team[$teamrow["Away"]] = $team[$teamrow["Away"]] + $points;
                }
                if ($teamrow["Result"] == "HomeForfeit"){
                    $team[$teamrow["Home"]] = $team[$teamrow["Home"]] - $points;
                    $team[$teamrow["Away"]] = $team[$teamrow["Away"]] + $points;
                }
                if ($teamrow["Result"] == "AwayForfeit"){
                    $team[$teamrow["Home"]] = $team[$teamrow["Home"]] + $points;
                    $team[$teamrow["Away"]] = $team[$teamrow["Away"]] - ($points/2);
                }
                if ($teamrow["Result"] == "DuelForfeit"){
                    $team[$teamrow["Home"]] = $team[$teamrow["Home"]] - ($points/2);
                    $team[$teamrow["Away"]] = $team[$teamrow["Away"]] - ($points/2);
                }
            }
        }   

这段代码在我的 MySql table 中运行并成功更新(不包括那部分代码)每次使用其中一行时我都会收到 "PHP Notice: Undefined index: " 错误.

这是不好的做法还是语法错误?我很困惑为什么它有效但仍然告诉我未定义的索引。

编辑: 出错的行总是我访问带有双数组的行,如“$team[$teamrow["Home"]]”。每当访问其中一个时,我都会收到错误消息。

编辑:我想我明白了。我正在一个尚不存在的数组中创建一个新键?

答:我在一个当时还不存在的关联数组中创建了一个新索引。此处无错误代码:

$团队=数组(); $计数器 = 0;

        $sql = "SELECT Home, Away, Result, Points FROM schedule";
        $schedulequery = mysqli_query($conn, $sql);

        // divy out the points
        if (mysqli_num_rows($schedulequery) > 0) {
            while($teamrow = mysqli_fetch_assoc($schedulequery)) {

                $points = $teamrow["Points"]; 
                if ($teamrow["Result"] == "Home"){
                    if (!isset($team[$teamrow["Home"]])){
                        $team[$teamrow["Home"]] = 0;
                    }

                    $team[$teamrow["Home"]] = $team[$teamrow["Home"]] + $points;
                }
                if ($teamrow["Result"] == "Tie"){
                    if (!isset($team[$teamrow["Home"]])){
                        $team[$teamrow["Home"]] = 0;
                    }
                    if (!isset($team[$teamrow["Away"]])){
                        $team[$teamrow["Away"]] = 0;
                    }
                    $team[$teamrow["Home"]] = $team[$teamrow["Home"]] + ($points/2);
                    $team[$teamrow["Away"]] = $team[$teamrow["Away"]] + ($points/2);
                }
                if ($teamrow["Result"] == "Away"){
                    if (!isset($team[$teamrow["Away"]])){
                        $team[$teamrow["Away"]] = 0;
                    }
                    $team[$teamrow["Away"]] = $team[$teamrow["Away"]] + $points;
                }
                if ($teamrow["Result"] == "HomeForfeit"){
                    if (!isset($team[$teamrow["Home"]])){
                        $team[$teamrow["Home"]] = 0;
                    }
                    if (!isset($team[$teamrow["Away"]])){
                        $team[$teamrow["Away"]] = 0;
                    }

                    $team[$teamrow["Home"]] = $team[$teamrow["Home"]] - $points;
                    $team[$teamrow["Away"]] = $team[$teamrow["Away"]] + $points;
                }
                if ($teamrow["Result"] == "AwayForfeit"){
                    if (!isset($team[$teamrow["Home"]])){
                        $team[$teamrow["Home"]] = 0;
                    }
                    if (!isset($team[$teamrow["Away"]])){
                        $team[$teamrow["Away"]] = 0;
                    }

                    $team[$teamrow["Home"]] = $team[$teamrow["Home"]] + $points;
                    $team[$teamrow["Away"]] = $team[$teamrow["Away"]] - ($points/2);
                }
                if ($teamrow["Result"] == "DuelForfeit"){
                    if (!isset($team[$teamrow["Home"]])){
                        $team[$teamrow["Home"]] = 0;
                    }
                    if (!isset($team[$teamrow["Away"]])){
                        $team[$teamrow["Away"]] = 0;
                    }

                    $team[$teamrow["Home"]] = $team[$teamrow["Home"]] - ($points/2);
                    $team[$teamrow["Away"]] = $team[$teamrow["Away"]] - ($points/2);
                }
            }
        }   

据我所知,问题可能是像 $team[$teamrow["Home"]] = $team[$teamrow["Home"]] + $points;.

这样的行

在代码的开头,您将 $team 初始化为一个没有索引或值的空数组。然后在你的 while 循环中你有像 $team[$teamrow["Home"]] = $team[$teamrow["Home"]] + $points; 这样的行,问题是 = 的右侧。在第一次迭代中将 $team[$teamrow["Home"]] 添加到 $points 的值是多少(当 $team 被认为是一个空数组时)?

由于您似乎使用的是整数,因此在 while 循环开始时我将执行以下操作

if(!isset($team[$teamrow["Home"]]))
{
    $team[$teamrow["Home"]] = 0;
}

同样,我也会对 $team[$teamrow["Away"]] 做同样的事情。这样,如果 $team[$teamrow["Home"]] 未定义,它将被赋予值 0