Menu
Coddy logo textTech

Recap: Data Processing

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

challenge icon

Challenge

Easy

You will receive three inputs: a comma-separated list of employee names, a comma-separated list of their corresponding work hours for the week, and a minimum hours threshold. The first input contains names in the format Alice,Bob,Charlie,Diana,Eve, the second input contains hours as numbers in the format 45,32,50,28,40, and the third input is the minimum hours threshold as a number (example: 35).

Read all three inputs and convert them into appropriate arrays. Create a combined associative array where employee names are keys and their work hours are values. Then perform the following operations using higher-order array functions:

  1. Use array_filter() with a custom callback function to keep only employees who worked at or above the minimum hours threshold
  2. Use array_map() with a custom callback function to calculate overtime pay for the filtered employees (overtime is any hours above 40, paid at 1.5x the rate of $25 per hour for regular hours)
  3. Use array_reduce() with a custom callback function to calculate the total overtime pay across all filtered employees

Print the results in the following format:

  • First, print each qualified employee's name and their overtime pay on separate lines in the format Name: $Amount (rounded to 2 decimal places)
  • Finally, print the total overtime pay in the format Total: $Amount (rounded to 2 decimal places)

Calculation details:

  • Regular hours (up to 40): $25 per hour
  • Overtime hours (above 40): $37.50 per hour (1.5x rate)
  • If an employee worked 40 hours or less, their overtime pay is $0.00

Input format:

  • First line: Comma-separated employee names (example: Alice,Bob,Charlie,Diana,Eve)
  • Second line: Comma-separated work hours as numbers (example: 45,32,50,28,40)
  • Third line: Minimum hours threshold as a number (example: 35)

Expected output: Each qualified employee with their overtime pay on separate lines, followed by the total overtime pay, all amounts formatted to 2 decimal places

Try it yourself

<?php
// Read inputs
$names = explode(',', trim(fgets(STDIN)));
$hours = explode(',', trim(fgets(STDIN)));
$minHours = intval(trim(fgets(STDIN)));

// Convert hours to integers
$hours = array_map('intval', $hours);

// Create associative array combining names and hours
$employees = array_combine($names, $hours);

// TODO: Write your code below
// 1. Use array_filter() to keep employees who worked at or above minimum hours
// 2. Use array_map() to calculate overtime pay for filtered employees
// 3. Use array_reduce() to calculate total overtime pay

// Output the results
// Print each qualified employee's overtime pay
// Print the total overtime pay

?>

All lessons in Logic & Flow