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

标签 php mysql arrays 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 表中工作并成功更新(不包括这部分代码),我收到“PHP 注意:未定义索引:”错误。

这是不好的做法还是错误的语法?我很困惑为什么它有效,但仍然告诉我 undefined index 。

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

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

最佳答案

答案:我正在一个尚不存在的关联数组中创建一个新索引。这里没有错误代码:

$team = 数组(); $计数器 = 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);
                }
            }
        }   

关于php - 使用带有键的数组作为数组键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38885880/

相关文章:

MySQL 子查询结果数组

php - 如何实现 "Report/Flag comment"

c++ - 检查数组中元素的相等性 - C++

php - ffmpeg mp4 视频无法在浏览器上的 html5 视频播放器上播放(格式损坏)

javascript - javascript 数组中有新行时出错

php - 拉取列表的 MailChimp PHP 脚本

javascript - 高效访问Array.prototype.filter单项返回

php - 选择和更新不工作——MySQL

mysql - 删除帐户表中的重复项并更新关联记录

php - 数组搜索的问题