PHP array_map() Function

PHP array_map Function


Definition :

The array_map() function sends each value of an array to a user-made function, and returns an array with new values, given by the user-made function.

Tip: You can assign one array to the function, or as many as you like.

Syntax

  array_map(myfunction, array1, array2, array3, ...)

Parameter Values
Parameter Description
myfunction Required. The name of the user-made function, or null
array1 Required. Specifies an array
array2 Optional. Specifies an array
array3 Optional. Specifies an array

Example1 : Send each value of an array to a function, multiply each value by itself, and return an array with the new values:

 

Output :

Array
(
    [0] => 1
    [1] => 4
    [2] => 9
    [3] => 16
    [4] => 25
)

Example2 : for user-made function to change the values of an array

Output :

Array
(
    [0] => Designing
    [1] => Designing
    [2] => House
)

Example3 : for using two arrays

Output :

Array
(
    [0] => different
    [1] => different
    [2] => same
)