Recap: Data Processing
Part of the Logic & Flow section of Coddy's PHP journey — lesson 45 of 68.
Challenge
EasyYou 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:
- Use
array_filter()with a custom callback function to keep only employees who worked at or above the minimum hours threshold - 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) - 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
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 Exercise3Sorting Arrays
Sort Indexed Arrays AscendingSort Indexed Arrays DescendingSort Assoc Arrays by ValueSort Assoc Arrays by KeyNatural Order SortingCustom Sorting with 'usort'Recap: Leaderboard Sorting6Higher-Order Array Functions
Transforming with 'array_map''array_map' with Custom FuncFiltering with 'array_filter''array_filter' with Custom FunReducing with 'array_reduce'Walking with 'array_walk'Recap: Data Processing