How do I use the `map()` function

The map() function in PHP is a built-in function that allows you to apply a callback function to each element of an array. It returns a new array containing the results of the callback function.

Keywords: PHP, map(), array, callback function
Description: The map() function provides a straightforward way to transform elements in an array by applying a specified function, thus enabling functional programming techniques in PHP.

$numbers = [1, 2, 3, 4, 5];
$squared = array_map(function($num) {
    return $num * $num;
}, $numbers);

print_r($squared); // Outputs: Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 [4] => 25 )
    

Keywords: PHP map() array callback function