'array_map' with Custom Func
Part of the Logic & Flow section of Coddy's PHP journey — lesson 40 of 68.
While built-in functions like strtoupper work well with array_map(), the real power comes from using your own custom functions. This allows you to create specific transformations tailored to your exact needs.
To use a custom function with array_map(), simply pass the function name as a string for the first parameter. Your custom function should accept one parameter (the current array element) and return the transformed value.
<?php
function doubleNumber($num) {
return $num * 2;
}
$numbers = [1, 2, 3, 4, 5];
$doubled = array_map("doubleNumber", $numbers);
// Result: [2, 4, 6, 8, 10]
?>This approach gives you complete control over how each element is processed. You can perform complex calculations, string manipulations, or any other logic within your custom function, making array_map() incredibly versatile for data transformation tasks.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyYou will receive two inputs: a comma-separated list of temperatures in Celsius and a target unit for conversion (either F for Fahrenheit or K for Kelvin). The first input contains temperatures as a string in the format 0,10,20,30,100, and the second input is a single character indicating the conversion type.
Read both inputs, convert the comma-separated temperatures into an array of numbers, create a custom function that converts a Celsius temperature to the target unit, use array_map() with your custom function to transform all temperatures, and print each converted temperature on a separate line, rounded to 2 decimal places.
Conversion formulas:
- Celsius to Fahrenheit:
(C × 9/5) + 32 - Celsius to Kelvin:
C + 273.15
Input format:
- First line: Comma-separated temperatures in Celsius (example:
0,10,20,30,100) - Second line: Target conversion unit - either
ForK(example:F)
Expected output: Each converted temperature printed on a separate line, rounded to 2 decimal places
Cheat sheet
Use custom functions with array_map() by passing the function name as a string. The custom function should accept one parameter (the current array element) and return the transformed value.
<?php
function doubleNumber($num) {
return $num * 2;
}
$numbers = [1, 2, 3, 4, 5];
$doubled = array_map("doubleNumber", $numbers);
// Result: [2, 4, 6, 8, 10]
?>This approach provides complete control over element processing, allowing complex calculations, string manipulations, or any custom logic within your function.
Try it yourself
<?php
// Read the comma-separated temperatures
$temperatures = fgets(STDIN);
// Read the target unit (F or K)
$unit = trim(fgets(STDIN));
// Convert the comma-separated string to an array of numbers
$tempArray = explode(',', trim($temperatures));
// TODO: Write your code below
// Create a custom function to convert Celsius to the target unit
// Use array_map() to apply the conversion to all temperatures
// Print each converted temperature on a separate line, rounded to 2 decimal places
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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