I looked into the similar topics in web as well stack overflow, but could get this one into my head clearly. Difference between array_map, array_walk and array_filter
<?php
error_reporting(-1);
$arr = array(2.4, 2.6, 3.5);
print_r(array_map(function($a) {
$a > 2.5;
},$arr));
print_r(array_filter($arr, function($a){
return $a > 2.5;
}));
?>
The above code returns me a filtered array whose value is > 2.5. Can i achieve what an array_filter
does with an array_map
?.
array_map
Returns an array containing all the elements of array after applying the callback function to each one.for example:
whereas
array_filter
return only those elements of array for which the function is trueexample: remove "bb" value from array
array_filter
returns the elements of the original array for which the function returns true.array_map
returns an array of the results of calling the function on all the elements of the original array.I can't think of a situation where you could use one instead of the other.
array_filter
works without a callable (function) being passed, whereas forarray_map
it is mandatory.e.g.
array_walk
changes the actual array passed in, whereasarray_filter
andarray_map
return new arrays, this is because the array is passed by reference.Example:
Result:
All three, array_filter, array_map, and array_walk, use a callback function to loop through an array much in the same way foreach loops loop through an $array using $key => $value pairs.
For the duration of this post, I will be referring to the original array, passed to the above mentioned functions, as $array, the index, of the current item in the loop, as $key, and the value, of the current item in the loop, as $value.
array_filter is likened to MySQL's SELECT query which SELECTs records but doesn't modify them.
array_filter's callback is passed the $value of the current loop item and whatever the callback returns is treated as a boolean.
If true, the item is included in the results.
If false, the item is excluded from the results.
Thus you might do:
array_map on the other hand accepts multiple arrays as arguments. If one array is specified, the $value of the current item in the loop is sent to the callback. If two or more arrays are used, all the arrays need to first be passed through array_values as mentioned in the documentation:
The first array is looped through and its value is passed to the callback as its first parameter, and if a second array is specified it will also be looped through and its value will be sent as the 2nd parameter to the callback and so-on and so-forth for each additional parameter.
If the length of the arrays don't match, the largest array is used, as mentioned in the documentation:
Each time the callback is called, the return value is collected. The keys are preserved only when working with one array, and array_map returns the resulting array. If working with two or more arrays, the keys are lost and instead a new array populated with the callback results is returned. array_map only sends the callback the $value of the current item not its $key. If you need the key as well, you can pass
array_keys($array)
as an additional argument then the callback will receive both the $key and $value.However, when using multiple arrays, the original keys will be lost in much the same manner as array_values discards the keys. If you need the keys to be preserved, you can use array_keys to grab the keys from the original array and array_values to grab the values from the result of array_map, or just use the result of array_map directly since it is already returning the values, then combine the two using array_combine.
Thus you might do:
array_walk is very similar to
foreach($array as $key=>$value)
in that the callback is sent both a key and a value. It also accepts an optional argument if you want to pass in a 3rd argument directly to the callback.array_walk returns a boolean value indicating whether the loop completed successfully.
(I have yet to find a practical use for it)
Note that array_walk doesn't make use of the callback's return. Since array_walk returns a boolean value, in order for array_walk to affect something, you'll need to reference &$value so you have that which to modify or use a global array. Alternatively, if you don't want to pollute the global scope, array_walk's optional 3rd argument can be used to pass in a reference to a variable with which to write to.
Thus you might do: