What are the differences between array_map(), array_walk() and array_filter() methods in PHP ?
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 |
Similar Reads
What are array_map(), array_reduce() and array_walk() function in PHP ?
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 applyi
3 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
Difference between array_merge() and array_combine() functions in PHP
array_merge() Function: The array_merge() function is used to merge two or more arrays into a single array. This function is used to merge the elements or values of two or more arrays together into a single array. The merging occurs in such a manner that the values of one array are appended at the e
3 min read
Difference Between Array.prototype.map() and Array.prototype.flatMap() in JavaScript
In JavaScript, Array.prototype.map() and Array.prototype.flatMap() are both the methods used to the transform elements in arrays but they differ in their behaviors regarding handling and flattening of the nested arrays.What is Array.prototype.map()?The map() method creates a new array populated with
3 min read
What is the difference between for and Foreach loop in PHP ?
Loops can be used to iterate over collection objects in PHP. The for and foreach loop can be used to iterate over the elements. for loopThe for loop works at the end of the given condition. It is used for the implementation of variables and works in a single way. The for loop does not work in the ca
3 min read
What is difference between __sleep and __wakeup() in PHP ?
In PHP, the __sleep and the __wakeup methods are called as magic methods. These methods are invoked or executed when we want to deal with serialization and deserialization of objects during runtime to store or save the object information in string format and again the information can be restored. _
4 min read
What is the difference between var_dump() and print_r() in PHP ?
In this article, we will discuss the difference between var_dump() and print_r() function in PHP. var_dump() Function: The var_dump() function is used to dump information about a variable that displays structured information such as the type and value of the given variable. Syntax: void var_dump ($e
3 min read
What are the different operations can be performed with a JavaScript Array?
In this article, we are going to learn different operations that can be performed with an Array by using JavaScript, An array in JavaScript is a data structure that holds an ordered collection of values, which can be of any data type, using zero-based indexing for access and manipulation, including
5 min read
Difference between Array and Map
Array:An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of t
12 min read
What are magic methods and how to use them in PHP ?
PHP magic methods are special methods that are called automatically when certain conditions are met. There are several magic methods in PHP. Every magic method follows certain rules - Every magic method starts with a double underscore ( __ ).They are predefined and neither can be created nor removed
4 min read