Skip to content
Related Articles

Related Articles

What are array_map(), array_reduce() and array_walk() function in PHP ?

View Discussion
Improve Article
Save Article
Like Article
  • 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 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!