Tag Archives: array_walk_recursive

How to apply a function to every array element in PHP

I often come across situations in which I need to apply some function to every element of an array in PHP. For example, to sanitize the user input, function like htmlentities can be used. We can easily use this function on a single variable or a single dimension array by simply applying the function or doing a foreach loop respectively. However, to do that on a multi-dimensional array is not very easy using foreach loop. In this article I will discuss the various methods we can use to apply the htmlentities function to a single variable and different types of arrays. The methods that I will discuss can be used to apply any default PHP function or any user defined function to every element of an array.

Read more »