Menu
Coddy logo textTech

Generating a Report Card

Part of the Logic & Flow section of Coddy's PHP journey — lesson 38 of 68.

challenge icon

Challenge

Easy

You will receive one input: a JSON object representing the student gradebook. The gradebook uses student names as keys, with each student having an "id" and "grades" array containing their test scores.

Read the input, convert the JSON string to a gradebook array, and generate a formatted report card for all students. For each student, display their name, student ID, all individual grades, and their average grade rounded to 2 decimal places.

Format the output exactly as follows:

  • Print ===== REPORT CARD ===== as the header
  • For each student, print:
    • Student: [name]
    • ID: [student_id]
    • Grades: [grade1], [grade2], [grade3], ... (all grades separated by commas and spaces)
    • Average: [average] (rounded to 2 decimal places)
    • A blank line after each student (except the last one)

Input format: One line containing a JSON object representing the gradebook (example: {"Alice":{"id":101,"grades":[85,92,78]},"Bob":{"id":102,"grades":[90,88,95]},"Charlie":{"id":103,"grades":[76,84,89]}})

Expected output: A formatted report card showing each student's information with their name, ID, all grades, and calculated average

Try it yourself

<?php
// Read the JSON input
$input = trim(fgets(STDIN));

// Convert JSON to array
$gradebook = (array)json_decode($input, true);

// Print the header
echo "===== REPORT CARD =====\n";

// TODO: Write your code below
// Loop through each student in the gradebook
// For each student:
//   - Print their name
//   - Print their ID
//   - Print all their grades
//   - Calculate and print their average (rounded to 2 decimal places)
//   - Add a blank line between students (except after the last one)

?>

All lessons in Logic & Flow