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 uses the callback to the elements of the given arrays.
Syntax
The syntax of the PHP array_map function is the following.
array_map(myfunction,array1,array2,array3...)
Parameters
The myfunction parameter is required, and it is the name of the User Defined Function.
An array1 parameter is a required parameter, and it specifies an array.
The array2 array3 are optional parameters, and it also specified the arrays.
Return Value
It returns an array containing the values of array1 after applying the user-made function to each one.
Example
See the following example.
<?php // app.php function square($x) { return($x * $x); } $arrA = array(19, 21, 46, 29); $arrO = array_map("square",$arrA); print_r($arrO);
We have defined one array and then passed 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.
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, we have to pass null as a parameter in place of the functionName parameter and the list of arrays to create an array. See the following example.
<?php // app.php $arrA = array(19, 21, 46, 29); $arrB = array('K', 'K', 'A', 'V'); $arrO = array_map(null, $arrA, $arrB); print_r($arrO);
The output is the following.
PHP array_map() Using Multiple Arrays
You can take three arrays, pass those arrays to an array_map function and see the output.
<?php // 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); print_r($arrO);
The output is the following.
PHP array_map() with string keys
You can pass the associative array to the array_map() function and see the output.
<?php // app.php function stormBreaker($a) { return $a."***".$a; } $arrA = ["name" => "Krunal", "age" => 26 ]; $arrO = array_map("stormBreaker", $arrA); print_r($arrO);
PHP array_map() using a lambda function
Lambda function means it has no name. See the following example.
<?php // 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.
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 can also receive the arbitrary number of arrays; it can iterate over them 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. However, this is 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.
That’s it for this tutorial.