PHP array_map function

Last Updated: February 13, 2022

array_map – Apply callback functions to elements of the array and return a new array with elements.

Syntax

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

Supports (PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

Parameters

array – the array you want to evaluate

Parameter Description
callback Required. function for the callback
array Required. array elemst are run through the callback function
arrays Suplimentary arrays that will run through callback function

Return Value

Returns an array

Example

To calculate the public value of the each element of the array

<?php
function cube($n)
{
    return ($n * $n * $n);
}

$a = [1, 2, 3, 4, 5];
$b = array_map('cube', $a);
print_r($b);

?>

Output

Array ( 
[0] => 1 
[1] => 8 
[2] => 27 
[3] => 64 
[4] => 125 
) 
Test