Menu
Coddy logo textTech

'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.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

You 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 F or K (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

?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow