PHP :: array_map [Execute a function on all elements of the array]

I had to trim all the values from the array. I wrote a loop that traversed through all the values in the array and pushed them to a different (new) array.

Later I came across this function array_map that accepts a callback function and the array – and applies the callback to all individual elements of the array. The loop was replaced by a single line of code which was pretty AWESOME !!!

$my_arr = array_map('trim',$my_arr);
Posted in PHP

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