What are array_map(), array_reduce() and array_walk() function in PHP ?
Last Updated :
25 Mar, 2022
In this article, we will see array_map(), array_reduce(), and array_walk() functions in PHP. We will see how these functions work along with understanding their basic implementation through the examples.
array_map() Function: The array_map() function returns an array containing the results of applying the callback to each value of the array used as arguments for the callback. In simple words, an array_map() function sends each value of an array to a user function and returns an array with new values. It’s really useful when you want to perform a specific operation on every element of an array. If you want to perform a specific action on each element of an array instead of iterating over each element of an array it is better to use an array_map() function which is built for this. An array_map() function returns an array containing the results of applying the callback function over the array.
Syntax:
array_map(function_name, array1, array2, array3, ...)
Parameters:
- function_name: A callable function to apply to each element in each array.
- array1: It is an array of elements to which the callback function applies.
Note: We can send multiple arrays in the array_map() function.
Example: In this example, we calculate the square of each element in an array using the array_map() function.
PHP
<?php
function square($n) {
return ($n * $n);
}
$a = [1, 2, 3, 4, 5];
$b = array_map('square', $a);
print_r($b);
?>
Output:
Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 [4] => 25 )
array_reduce() Method: As the name suggests, an array_reduce() function reduces the array to a single value by performing the given operation. The array_reduce() applies the callback function to the elements of the array and gives output as a single value. This function was introduced in PHP 4.0.5.
Syntax:
array_reduce(array, myfunction, initial)
Parameters:
- array: It is the input array that will be reduced to a single value.
- myfunction: It is a callback function that determines how the array should be reduced.
- initial: It is an optional value that will be used at the beginning of the process, or as a final result in case the array is empty.
Example: In the example, we are getting the addition of an array as a single variable.
PHP
<?php
function add($num1, $num2) {
$num1 += $num2;
return $num1;
}
$a = array(1, 2, 3, 4, 5, 6);
$num1 = array_reduce($a, "add");
echo $num1;
?>
Output:
21
array_walk() Method: It applies a user-defined function to every member of an array. The array's keys and values are parameters in the function. The array_walk() function is not affected by the internal array pointer of the array. It will traverse through all the elements. The array_map() cannot operate with the array keys, while the array_walk() function can work with the key values pair.
Syntax:
array_walk(array, myfunction, parameter...)
Parameters:
- array: The input array.
- myfunction: Name of the function
- parameter: Specifies a parameter to the user-defined function. You can assign multiple parameters.
Example:
PHP
<?php
function myfunction($value,$key) {
echo "Geeksforgeeks $key is about $value \n";
}
$articles = array(
"article-1" => "HTML",
"article-2" => "CSS",
"article-3" => "PHP"
);
array_walk($articles,"myfunction");
?>
Output:
Geeksforgeeks article-1 is about HTML
Geeksforgeeks article-2 is about CSS
Geeksforgeeks article-3 is about PHP
Similar Reads
What are the differences between array_map(), array_walk() and array_filter() methods in PHP ? array_map() Method: The array_map() is used to modify all elements in one or more arrays according to some user-defined condition in an easy manner. It basically sends each of the elements of an array to a user-defined function and returns an array with new values as modified by that function. Synta
5 min read
What is the differences between array_merge() and array_merge_recursive() functions in PHP ? In this article, we will see the array_merge() and array_merge_recursive() functions, along with understanding their basic implementation, & the differences between them. Both the array_merge() Function and array_merge_recursive() function can be used to combine multiple arrays into a single arr
3 min read
What is the use of array_count_values() function in PHP ? In this article, we will discuss array_count_values() function & its usage in PHP, along with understanding its basic implementation through the illustrations. The array_count_values() function is used to count all the values inside an array. In other words, we can say that array_count_values()
2 min read
PHP array_walk_recursive() Function The array_walk_recursive() function is an inbuilt function in PHP. The array_walk_recursive() function walks through the entire array regardless of pointer position and applies a callback function or user-defined function to every element of the array recursively. The array elementâs keys and values
2 min read
PHP array_reduce() Function This inbuilt function of PHP is used to reduce the elements of an array into a single value that can be of float, integer or string value. The function uses a user-defined callback function to reduce the input array. Syntax: array_reduce($array, own_function, $initial) Parameters: The function takes
2 min read