Skip to content
Related Articles

Related Articles

What are the differences between array_map(), array_walk() and array_filter() methods in PHP ?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 16 May, 2022

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.

Syntax:

array_map(functionName, arr1, arr2...)

Parameters: This function takes 2 compulsory parameter functionName and arr1 and the rest are optional.

  • functionName(mandatory): This parameter defines the name of the user-defined function according to which the values in the array will be modified.
  • arr1(mandatory): This parameter specifies the array to be modified.
  • arr2(mandatory): This parameter specifies the array to be modified.

The functionName parameter is compulsory and we can pass any number of arrays to this function named arr1, arr2, …, arrn, and so on.

Return Value: This function returns an array containing all the elements of arr1 after applying the user function to each one.

Example: Below example illustrates the working of the array_map() function in PHP.

PHP




<?php
  
function fun1($v) {
      return ($v + 7);     // add 7
}
  
function fun2($v1, $v2) {
    if ($v1 == $v2) return 1;    
    else return 0;
}
  
$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(1, 3, 3, 4, 8);
  
print_r(array_map("fun1", $arr1));
  
print_r(array_map("fun2", $arr1, $arr2));
  
?>


Output:

Array
(
    [0] => 8
    [1] => 9
    [2] => 10
    [3] => 11
    [4] => 12
)
Array
(
    [0] => 1
    [1] => 0
    [2] => 1
    [3] => 1
    [4] => 0
)

array_walk() Method: The array_walk() method walks through the entire array regardless of pointer position and applies a callback function or user-defined function to every element of the array. The array element’s keys and values are parameters in the callback function.

Syntax:

boolean array_walk($array, myFunction, $extraParam)

Parameters: This function accepts three parameters as described below.

  • $array: This is a mandatory parameter and specifies the input array.
  • myFunction: This parameter specifies the name of the user-defined function and is also mandatory. The user-defined function generally excepts two parameters of which the first parameter represent the array’s values and the second parameter represents the corresponding keys.
  • $extraparam: This is an optional parameter. It specifies an extra parameter to the user-defined function in addition to the two parameters, array keys, and values.

Return Value: This function returns a boolean value. It returns TRUE on success or FALSE on failure.

Example: Below example illustrates the array_walk() method.

PHP




<?php
// PHP program to illustrate array_walk() method
  
// User-defined callback function
function myfunction($value, $key) {
    echo "The key $key has the value $value </br>";
}
  
// Input array
$arr = array(
      "a" => "yellow"
      "b" => "pink"
      "c" => "purple"
);
  
// Calling array_walk() method with
// no extra parameter
array_walk($arr, "myfunction");
  
?>


Output:

The key a has the value yellow 
The key b has the value pink 
The key c has the value purple

array_filter() Method: This method is used to filter the elements of an array using a user-defined function which is also called a callback function. The array_filter() function iterates over each value in the array, passing them to the user-defined function or the callback function. If the callback function returns true then the current value of the array is returned into the result array otherwise not. The keys of the array get preserved, i.e. the key of the element in the original array and output array is the same.

Syntax:

array array_filter($array, $callback_function, $flag)

Parameters: The function takes three parameters, out of which one is mandatory and the other two are optional.

  • $array (mandatory): This refers to the input array on which the filter operation is to be performed.
  • $callback_function (optional): Refers to the user-defined function. If the function is not supplied then all entries of the array equal to FALSE will be removed.
  • $flag (optional): Refers to the arguments passed to the callback function.
    • ARRAY_FILTER_USE_KEY – passes key as the only argument to a callback function, instead of the value of the array.
    • ARRAY_FILTER_USE_BOTH – passes both value and key as arguments to callback instead of the value.

Return Value: The function returns a filtered array.

Example: Below is a program showing how to return or filter out even elements from an array using the array_filter() function.

PHP




<?php
  
// PHP function to check for even
// elements in an array
function Even($array) {
    
    // Returns if the input integer is even
    if($array % 2 == 0)
        return TRUE;
    else
        return FALSE;
}
  
$array = array(12, 0, 0, 18, 27, 0, 46);
print_r(array_filter($array, "Even"));
  
?>


Output:

Array
(
    [0] => 12
    [1] => 0
    [2] => 0
    [3] => 18
    [5] => 0
    [6] => 46
)

Differences:

array_map() array_walk () array_filter()
This function applies the callback to the elements present in the array. This function applies the user-defined callback function to each element of the given input array. This function is used to filter the elements present in an array using a callback function.
This callback function of array_map() runs for each element in each array. array_walk() function applies a user-supplied function to every member of an array The array_filter() iterates over each value in the array passing them to the callback function
 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!