Generating a Report Card
Part of the Logic & Flow section of Coddy's PHP journey — lesson 38 of 68.
Challenge
EasyYou 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
1Advanced Functions
Anonymous FunctionsClosures and 'use'Arrow FunctionsCallback FunctionsUsing 'call_user_func'Variable FunctionsPassing by ReferenceRecursive FunctionsRecap: Function Medley4Multi-dimensional Arrays
Creating a 2D ArrayAccessing 2D Array ElementsModifying 2D Array ElementsIterating with Nested Loops2D Associative ArraysRecap: Simple Grid Exercise2Advanced Array Manipulations
Adding with 'array_push'Removing with 'array_pop'Adding with 'array_unshift'Removing with 'array_shift'Merging Indexed ArraysMerging Associative ArraysExtracting with 'array_slice'Values with 'in_array'Keys with 'array_search'Recap: Playlist Exercise5Student Gradebook
Project Setup: Data StructureAdding a New StudentAdding a Grade to a StudentCalculating Student's AverageFinding the Top StudentGenerating a Report Card3Sorting Arrays
Sort Indexed Arrays AscendingSort Indexed Arrays DescendingSort Assoc Arrays by ValueSort Assoc Arrays by KeyNatural Order SortingCustom Sorting with 'usort'Recap: Leaderboard Sorting