Menu
Coddy logo textTech

Finding the Top Student

Part of the Logic & Flow section of Coddy's PHP journey — lesson 37 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, find the student with the highest average grade, and print the name of that top student.

To find the top student: calculate the average grade for each student by summing their grades and dividing by the number of grades, then determine which student has the highest average.

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: The name of the student with the highest average grade (example: Bob)

Try it yourself

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

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

// TODO: Write your code below
// Calculate the average grade for each student
// Find the student with the highest average


// Output the name of the top student
echo $topStudent;
?>

All lessons in Logic & Flow