What are the advantages of using a foreach loop compared to array_map for unit conversion in PHP?

When converting units in PHP, using a foreach loop allows for more flexibility and control over the conversion process compared to using array_map. With a foreach loop, you can easily access and modify each element of the array individually, making it easier to handle complex conversion logic or conditions. Additionally, foreach loops are generally more readable and easier to understand, especially for developers who may not be familiar with the array_map function.

// Sample array of temperatures in Celsius
$temperatures = [0, 10, 20, 30];

// Conversion function from Celsius to Fahrenheit
function celsiusToFahrenheit($celsius) {
    return ($celsius * 9/5) + 32;
}

// Using a foreach loop to convert temperatures
$fahrenheitTemperatures = [];
foreach ($temperatures as $celsius) {
    $fahrenheit = celsiusToFahrenheit($celsius);
    $fahrenheitTemperatures[] = $fahrenheit;
}

// Output the converted temperatures
print_r($fahrenheitTemperatures);