views:

126

answers:

3

Hi,

What exactly is the the difference between array_map, array_walk and array_filter. What I could see from documentation is that you could pass a callback function to perform an action on the supplied array. But I don't seem to find any particular difference between them.

Do they perform the same thing?
Can they be used interchangeably?

I would appreciate your help with illustrative example if they are different at all.

Thanks

+5  A: 
  • array_map has no collateral effects while array_walk can have; in particular, array_map does never change its arguments.
  • array_map cannot operate with the array keys, array_walk can.
  • array_map returns an array, array_walk only returns true/false. Hence, if you don't want to create an array as a result of traversing one array, you should use array_walk.
  • array_map also can receive an arbitrary number of arrays, while array_walk operates only on one.
  • The resulting array of array_map/array_walk has the same number of elements as the argument(s); array_filter picks only a subset of the elements of the array according to a filtering function. It does not preserve the keys.

Example:

<pre>
<?php
$origarray1 = array(2.4, 2.6, 3.5);
$origarray2 = array(2.4, 2.6, 3.5);

print_r(array_map('floor', $origarray1)); //$origarray1 stays the same

//changes $origarray2
array_walk($origarray2, function (&$v, $k) { $v = floor($v); }); 
print_r($origarray2);

//this is a more proper use of array_walk
array_walk($origarray1, function ($v, $k) { echo "$k => $v", "\n"; });

//array_map accepts several arrays
print_r(
    array_map(function ($a, $b) { return $a * $b; }, $origarray1, $origarray2)
);

//select only elements that are > 2.5
print_r(
    array_filter($origarray1, function ($a) { return $a > 2.5; })
);

Result:

Array
(
    [0] => 2
    [1] => 2
    [2] => 3
)
Array
(
    [0] => 2
    [1] => 2
    [2] => 3
)
0 => 2.4
1 => 2.6
2 => 3.5
Array
(
    [0] => 4.8
    [1] => 5.2
    [2] => 10.5
)
Array
(
    [1] => 2.6
    [2] => 3.5
)
Artefacto
Could you show them with an example please?
Web Logic
Great, thanks :)
Web Logic
A: 

From the documentation, array_walk takes an array and a function F and modifies it by replacing every element x with F(x). array_map does the exact same thing except that instead of modifying in-place it will return a new array with the transformed elements. array_filter with function F, instead of transforming the elements, will remove any elements for which F(x) is not true

Steven Schlansker
Could you show them with an example please?
Web Logic
A: 

The idea of mapping an function to array of data comes from functional programming. You shouldn't think about array_map as a foreach loop that calls a function on each element of the array (even though that's how it's implemented). It should be thought of as applying the function to each element in the array independently.

In theory things such as function mapping can be done in parallel since the function being applied to the data should ONLY effect the data and NOT the global state. This is because an array_map could choose any order in which to apply the function to the items in (even though in PHP it doesn't).

array_walk on the other hand it the exact opposite approach to handling arrays of data. Instead of handling each item separately, it uses a state (&$userdata) and can edit the item in place (much like a foreach loop). Since each time an item has the $funcname applied to it, it could change the global state of the program and therefor requires a single correct way of processing the items.

Back in PHP land, array_map and array_walk are almost identical except array_walk gives you more control over the iteration of data and is normally used to "change" the data in-place vs returning a new "changed" array.

array_filter is really an application of array_walk and it more-or-less just provided for convenience.

Kendall Hopkins