180 of 410 menu

The array_map Function

The array_map function applies a given function to all elements of an array and returns the modified array. The first parameter is the callback function, and the second is the array. Additional arrays can be passed as the third and subsequent parameters.

Syntax

array_map(?callable $callback, array $array, array ...$arrays): array

Example

Let's extract the square root of each element of the array (using the sqrt function) and write it to a new array:

<?php $arr = [1, 4, 9]; $res = array_map('sqrt', $arr); var_dump($res); ?>

Code execution result:

[1, 2, 3]

See Also

byenru