Array Mapping in PHP
You can transform the values of an array using the array_map()
function, which applies a callback function to each element of the array.
Example of Array Mapping
<?php
$numbers = array(1, 2, 3, 4, 5);
// Square each number
$squared_numbers = array_map(function($number) {
return $number * $number;
}, $numbers);
print_r($squared_numbers);
?>
Output:
Array
(
[0] => 1
[1] => 4
[2] => 9
[3] => 16
[4] => 25
)
Explanation: This example demonstrates how to use array_map()
to square each element in an array.