# Explain array_map() function in PHP

PHPProgrammingServer Side Programming

PHP offers various types of inbuilt functions to perform distinctive functionalities. The array_map() is an inbuilt function in PHP and it works with PHP array.

This function works in such a way that it sends every component of an array to a predefined function and returns an array with new values as modified by that function.

This function changes all elements of at least one array as indicated by some user-defined condition in a simple way.

## Syntax

array_map (function name, array1,array2...)

## Parameters

This function takes 2 compulsory parameters one is function name and another is an array and the rest are discretionary.

## functionname(mandatory)

This parameter characterizes the name of the user-defined function as per which the values in the array to be changed.

## array1(mandatory)

This parameter determines the array to be changed.

## Example

Live Demo

<?php
function add($arr){ return ($arr+ 2);
}
$arr1 = array(7, 6, 2, 4); print_r(array_map("add",$arr1));
?>

## Output

Array
(
[0] => 9
[1] => 8
[2] => 4
[3] => 6
)

## Explanation

In the above example, we have defined a function which takes the input as an array and adds 2 to every element of that array

Updated on 29-Jun-2020 09:20:55