3

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);
6
  • 1
    Can you share an example of your array structure Commented Mar 31, 2014 at 1:15
  • And you're sure it's not $course[$i]['credit_hours'] causing the issue? Commented Mar 31, 2014 at 1:27
  • Can you show examples of $course, $i and $grade_conversions at the point of the error? Best example would be to add var_dump($course, $i, $grade_conversions) above line 127 Commented Mar 31, 2014 at 1:50
  • Could be your $course array isn't using contiguous indexes. You can probably simplify that loop with foreach ($course as $courseItem), then use $courseItem in place of $course[$i] Commented Mar 31, 2014 at 2:06
  • 2
    Also, going to go out on a limb here and guess that you have some unexpected whitespace in your $course[$i]['grade'] value. Try $grade_conversion[trim($course[$i]['grade'])] Commented Mar 31, 2014 at 2:09

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

3 Comments

That's definitely a more efficient way to build the array. Thanks again man. Making my code more concise and readable is precisely what I'm trying to get better at and this is definitely another step in the right direction. Thanks for the advice.
I'm not very experienced with reading/writing to files, which is probably why this was causing me so much difficulty. Hopefully I can learn how to do it in the formats you mentioned in the future.
@user3479482 I just added a quick JSON example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.