Improve code readability with PHP’s array_map() function. Instead of looping through items to change them, apply a callback to each element in one go.
$numbers = [1, 2, 3, 4, 5];
// Apply a function to each element of the array
$squaredNumber = array_map(function ($number) {
return number * number;
}, $numbers);
// Output: 1, 4, 9, 16, 25
PHPThe array_map
function will iterate through the $numbers
as $number
. So $number
will have each index value of $numbers
.