Latest Code Tutorials

PHP array_map: How to Modify All Array Items in PHP

The array_map() function helps to modify all the elements of one or more arrays according to some user-specific conditions straightforwardly.

PHP array_map()

The array_map() is a built-in PHP function that sends each value of an array to the user-defined function and returns the array with new values given by the user-defined function.

The array_map() function sends each value of an array to a user-defined function and gets an array with new values applied by the user-defined function then, and it applies the callback to the elements of the given arrays.


The syntax of the PHP array_map function is the following.



The myfunction parameter is required, and it is the name of the User Defined Function.

An array1 parameter is a required parameter, and it specified an array.

The array2, array3 are optional parameters, and it specified the arrays as well.

Return Value

It returns an array containing the values of array1 after applying the user-made function to each one.


See the following example.


// app.php

function square($x)
  return($x * $x);
$arrA = array(19, 21, 46, 29);
$arrO = array_map("square",$arrA);

We have defined one array and then pass that array to the array_map function, and that function returns the square of all the items of the array and forms a new array with that values.

The output of the above code is the square of all the items. See the below output.

PHP Array Map Example

Creating an array of arrays using array_map()

We can also use an array_map() function in PHP to create an array of arrays.

If we want to do this, then we have to pass null as a parameter in place of the functionName parameter and the list of arrays to create an array of arrays. See the following example.


// app.php

$arrA = array(19, 21, 46, 29);
$arrB = array('K', 'K', 'A', 'V');
$arrO = array_map(null, $arrA, $arrB);

The output is the following.

Creating an array of arrays using array_map()

PHP array_map() Using Multiple Arrays

You can take three arrays and pass those arrays to an array_map function and see the output.


// app.php

function subtractAdd($a, $b, $c) {
  return $a - $b + $c;

$arrA = array(19, 21, 46, 29);
$arrB = array(18, 19, 20, 21);
$arrC = array(1, 2, 26, 8);
$arrO = array_map("subtractAdd", $arrA, $arrB, $arrC);

The output is the following.

The array_map() Using Multiple Arrays

PHP array_map() with string keys

You can pass the associative array to the array_map() function and see the output.


// app.php

function stormBreaker($a) {
  return $a."***".$a;

$arrA = ["name" => "Krunal", "age" => 26 ];
$arrO = array_map("stormBreaker", $arrA);
The output is the following.

The array_map() with string keys

PHP array_map() using a lambda function

Lambda function means it has no name. See the following example.


// app.php

$func = function($value) {
  return $value * 2;

print_r(array_map($func, range(1, 5)));

We have used the $func as lambda function and range() function to provide the value between 1 to 5. See the below output.

array_map() using a lambda function

Finally, PHP Array Map Example | array_map() Function Tutorial is over.

Difference between array_map(), array_walk() and array_filter()

What exactly is the difference between array_map, array_walk, and array_filter? Well, see the below answer.

Changing Values:

The array_map() function cannot change the values inside the input array(s) while array_walk() can; in particular, array_map() function never changes its arguments.

Array Keys Access:

The array_map() function cannot operate with the array keys, PHP array_walk() function can.

Return Value:

The array_map() function returns the new array, array_walk() function only returns true.

Hence, if you don’t want to create the array resulting from traversing one array, you should use the array_walk() function.

Iterating Multiple Arrays:

The array_map() function also can receive the arbitrary number of arrays, and it can iterate over them in parallel, while the array_walk() function operates only on one.

Passing Arbitrary Data to Callback:

PHP array_walk() function can receive the extra arbitrary parameter to pass to the callback. This mostly irrelevant since PHP 5.3.

Length of Returned Array:

The resulting array of array_map() function has the same length as that of the largest input array; array_walk() function does not return an array, but at the same time it cannot alter the number of elements of an original array;

PHP array_filter() function picks only a subset of the array elements according to a filtering function. However, it does preserve the keys.

Finally, the PHP array_map() function example is over.

Related Posts

PHP array_keys()

PHP array_push()

PHP array_pop()

PHP array_shift()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.