How to use array_map() function in php?
The array_map() function returns an array containing the results of applying the callback to each value of the array used as arguments for the callback.
1. Syntax:
Syntax:
array_map(function_name, array1, array2, array3, ...)
Parameters:
- function_name: A callable function to apply to each element in each array.
- array1: It is an array of elements to which the callback function applies.
Note: We can send multiple arrays in the array_map() function.
2. Example:
Code:
<?php
function square($n) {
return ($n * $n);
}
$a = [1, 2, 3, 4, 5];
$b = array_map('square', $a);
print_r($b);
?>
Output:
Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 [4] => 25 )