dongxianglun5163 2014-03-31 01:12
浏览 54
已采纳

带有嵌套索引的PHP“未定义索引”多维数组

I actually had this code working just fine, but I did some things to make it look a lot more clean and concise by using array keys. You can see how my first go at it is kind of sloppy looking with the keys being $classes[$i+3] etc. It works fine, but I wanted to make it more readable and make more sense at a glance.

This is the error message:

Notice: Undefined index: A in C:\xampp\htdocs\CreateaTranscript.php on line 127 ("A" is a key in the "$grade_conversion" array that relates to a decimal value for computing the user's GPA.)

This is the line of code I use to have that actually worked:

$grade_points += $grade_conversion[$classes[$i+3]] * $classes[$i+2];

This is the line of code causing the error:

$grade_points += $grade_conversion[$course[$i]['grade']] * $course[$i]['credit_hours'];

I tried enclosing the multi-dimensional array in 's. However, it just creates a different error that prevents the script from running. I know the basic issue is that the compiler sees an array key not enclosed in 's. However, why does it work with the normal array and not the multi-dimensional array?

$grade_conversion=array(
    'A' => 4,
    'A-' => 3.7,
    'B+' => 3.3,
    'B' => 3.0,
    'B-' => 2.7,
    'C+' => 2.3,
    'C' => 2.0,
    'C-' => 1.7,
    'D+' => 1.3,
    'D' => 1,
    'D+' => 0.7,
    'F' => 0,
);

I was trying to keep this concise. Is it possible to use an index in the manner I declared it? It worked fine before I switched it over to a multi-dimensional array. I'm going to add a sample of the for statement that the line was from:

for($i = 0;$i < count($course);$i++){
print "<tr bgcolor = 'F8F8F8'><td>" . $course[$i]['course_id'] . "<td>" . $course[$i]['course_name'] . "</td><td>" . $course[$i]['credit_hours'] . "</td><td>" . $course[$i]['grade'] . "</td></tr>";

//Keep a running count of credit hours for the GPA equation.
//Keep a running total of grade points using the $grade_conversion array
$total_credit_hours += $course[$i]['credit_hours'];
$grade_points += $grade_conversion[$course[$i]['grade']] * $course[$i]['credit_hours'];

}

The $course array is create by reading data from a file.

Here is how the $course array is created. The data is pulled from a data file line by line.

            $fh=fopen($file, "r");
        $j = 0;
        while (!feof($fh)){
                $course[$j]['course_id'] = fgets($fh);
                $course[$j]['course_name'] = fgets($fh);
                $course[$j]['credit_hours'] = fgets($fh);
                $course[$j]['grade'] = fgets($fh);
                $j++;
        }
        fclose($fh);
  • 写回答

1条回答 默认 最新

  • dongtuo3370 2014-03-31 02:18
    关注

    As mentioned in my comment above, your values will include some whitespace characters thanks to fgets. You'll need to trim() them when you store them in the the $course array or when using them as an index for $grade_conversions.

    You can also avoid manually specifying array indexes when populating your array, eg

    while (!feof($fh)){
        $course[] = [
            'course_id' => trim(fgets($fh)),
            'course_name' => trim(fgets($fh)),
            'credit_hours' => trim(fgets($fh)),
            'grade' => trim(fgets($fh))
        ];
    }
    

    and iterating it

    foreach ($course as $courseItem) {
        $total_credit_hours += $courseItem['credit_hours'];
        // etc
    }
    

    And finally, I think just about any structured text format (JSON, XML, CSV) would be better than un-keyed, newline separated values, eg

    // file.json
    [{
        "id": 1,
        "name": "Foo",
        "credit_hours": 2,
        "grade": "A"
    },
    {
        "id": 2,
        "name": "Bar",
        "credit_hours": 14,
        "grade": "B"
    }]
    

    and load it...

    $courses = json_decode(file_get_contents('file.json'), true);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的