Three Handy Array Functions and Their Nuances

10 04 2011

by PurplePaisleyPatch

Sometimes it may seem unclear what the differences are between array_map, array_filter and array_walk, since all three functions allow for altering an array with a callback.  You may wish to review the example that I provide of using each at

In brief, array_filter is concerned about removing something from an array. In the example, I provided, an array is filtered of any numbers so that only string data remains. Array_map and array_walk can be a little confusing because they have different return values and they have a different order of parameters with respect to the array and callback parameters.

Think of array_walk as allowing you to traverse the array with the option of changing in place each element if you provide a third parameter. The return value will be a boolean so that can be handy if you want to test if things went well.

Array_map is a functional programming type function that allows you to do the same sort of operation on each element of the array. What’s great about this function is that array_map will return a new array reflecting the changes for each array element while the original array is unaffected.

This work is licensed under a Creative Commons License




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: