- How to apply a function to every array element in PHP
- Apply htmlentites to the value of a variable:
- Apply htmlentities to single dimensional array:
- Apply htmlentities to two-dimensional array.
- Apply htmlentites to multidimensional array:
- Share this:
- PHP Executing Upon an Array Applying a function to each element of an array
- How to apply a function to every array element in PHP
- Apply htmlentites to the value of a variable:
- Apply htmlentities to single dimensional array:
- Apply htmlentities to two-dimensional array.
- Apply htmlentites to multidimensional array:
- Share this:
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.
Here are the various example of how we can apply the function on different inputs along with the output of the variable after the function is applied. In all these examples I will use htmlentities functions as an example function, as it is easy to identify if the function worked or not. htmlentities function converts all applicable characters to HTML entities. In the following examples if the ‘< ‘ and ‘>’ are converted into ‘<‘ and ‘>’ respectively, that means the function got applied correctly.‘>
Apply htmlentites to the value of a variable:
$var = 'some value is bold'; $var = htmlentities($var); var_dump($var);
string(47) "some value is <strong>bold</strong>"
Apply htmlentities to single dimensional array:
$array = array(); $array['key1'] = 'value 1'; $array['key2'] = 'value 2'; foreach($array as $key => $val) < $array[$key] = htmlentities($val); >var_dump($array);
array(2) < ["key1"]=>string(36) "value <strong>1</strong>" ["key2"]=> string(36) "value <strong>2</strong>" >
Apply htmlentities to two-dimensional array.
Method 1: Use array_map function. This method works only if the array is always a two-dimensional array.
$array = array(); $array['key1']['key2'] = 'value 1'; $array['key1']['key3'] = 'value 2'; foreach($array as &$val) < $val = array_map('htmlentities', $val); >var_dump($array);
array(1) < ["key1"]=>&array(2) < ["key2"]=>string(36) "value <strong>1</strong>" ["key3"]=> string(36) "value <strong>2</strong>" > >
Method 2: There is another way to apply htmlentities to a two-dimensional array without using foreach loop.
$array = array(); $array['key1']['key2'] = 'value 1'; $array['key1']['key3'] = 'value 2'; array_walk_recursive($array, function (&$value) < $value = htmlentities($value); >); var_dump($array);
array(1) < ["key1"]=>array(2) < ["key2"]=>string(36) "value <strong>1</strong>" ["key3"]=> string(36) "value <strong>2</strong>" > >
Apply htmlentites to multidimensional array:
In the method 1 to apply the function to a 2-dimensional array, I have used array_map function. However, array_map does not works with multidimensional arrays. Also, array_map does not works recursively hence we need a loop. Let’s see what we will get if we try to use this method on a multidimensional array:
$array = array(); $array['key1']['key11'] = 'value 11'; $array['key1']['key12'] = 'value 12'; $array['key2']['key13'] = 'value 13'; $array['key2'] = 'value 21'; $array['key3']['key31']['key311'] = 'value 311'; foreach($array as &$val) < $val = array_map('htmlentities', $val); >var_dump($array);
Warning: array_map() [function.array-map]: Argument #2 should be an array in test.php on line 10 Warning: htmlentities() expects parameter 1 to be string, array given in test.php on line 10 array(3) < ["key1"]=>array(2) < ["key11"]=>string(37) "value <strong>11</strong>" ["key12"]=> string(37) "value <strong>12</strong>" > ["key2"]=> NULL ["key3"]=> &array(1) < ["key31"]=>NULL > >
As we saw in the above example, we didn’t get the expected output and also got couple warnings. It didn’t work recursively on all elements of the array. Hence, if you are not sure about the dimensions of the array or if the array has different dimensions at each level then you cannot use array_map. In this case we can use the array_walk_recursive function. The array_walk_recursive function works recursively on the array and hence we don’t need any for loop for it.
$array = array(); $array['key1']['key11'] = 'value 11'; $array['key1']['key12'] = 'value 12'; $array['key2']['key13'] = 'value 13'; $array['key2'] = 'value 21'; $array['key3']['key31']['key311'] = 'value 311'; array_walk_recursive($array, function (&$value) < $value = htmlentities($value); >);
array(3) < ["key1"]=>array(2) < ["key11"]=>string(37) "value <strong>11</strong>" ["key12"]=> string(37) "value <strong>12</strong>" > ["key2"]=> string(37) "value <strong>21</strong>" ["key3"]=> array(1) < ["key31"]=>array(1) < ["key311"]=>string(38) "value <strong>311</strong>" > > >
Note: All the examples above work on PHP 5.3+. They won’t work in PHP versions less than PHP 5.3.0, because they do not support anonymous functions. So to make it work with PHP version < 5.3.0 the above code can be modified as follows:
$array = array(); $array['key1']['key11'] = 'value 11'; $array['key1']['key12'] = 'value 12'; $array['key2']['key13'] = 'value 13'; $array['key2'] = 'value 21'; $array['key3']['key31']['key311'] = 'value 311'; function my_apply_htmlentities(&$value) < $value = htmlentities($value); >array_walk_recursive($array, 'my_apply_htmlentities'); var_dump($array);
array(3) < ["key1"]=>array(2) < ["key11"]=>string(37) "value <strong>11</strong>" ["key12"]=> string(37) "value <strong>12</strong>" > ["key2"]=> string(37) "value <strong>21</strong>" ["key3"]=> array(1) < ["key31"]=>array(1) < ["key311"]=>string(38) "value <strong>311</strong>" > > >
Note: I have tested all the above codes and all codes (except for the last) work on PHP 5.3.13 and the last one works on PHP 5.2.11
Related Links:
Share this:
PHP Executing Upon an Array Applying a function to each element of an array
To apply a function to every item in an array, use array_map() . This will return a new array.
$array = array(1,2,3,4,5); //each array item is iterated over and gets stored in the function parameter. $newArray = array_map(function($item) < return $item + 1; >, $array);
$newArray now is array(2,3,4,5,6); .
Instead of using an anonymous function, you could use a named function. The above could be written like:
function addOne($item) < return $item + 1; >$array = array(1, 2, 3, 4, 5); $newArray = array_map('addOne', $array);
If the named function is a class method the call of the function has to include a reference to a class object the method belongs to:
class Example < public function addOne($item) < return $item + 1; >public function doCalculation() < $array = array(1, 2, 3, 4, 5); $newArray = array_map(array($this, 'addOne'), $array); >>
Another way to apply a function to every item in an array is array_walk() and array_walk_recursive() . The callback passed into these functions take both the key/index and value of each array item. These functions will not return a new array, instead a boolean for success. For example, to print every element in a simple array:
$array = array(1, 2, 3, 4, 5); array_walk($array, function($value, $key) < echo $value . ' '; >); // prints "1 2 3 4 5"
The value parameter of the callback may be passed by reference, allowing you to change the value directly in the original array:
$array = array(1, 2, 3, 4, 5); array_walk($array, function(&$value, $key) < $value++; >);
For nested arrays, array_walk_recursive() will go deeper into each sub-array:
$array = array(1, array(2, 3, array(4, 5), 6); array_walk_recursive($array, function($value, $key) < echo $value . ' '; >); // prints "1 2 3 4 5 6"
Note: array_walk and array_walk_recursive let you change the value of array items, but not the keys. Passing the keys by reference into the callback is valid but has no effect.
PDF — Download PHP for free
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.
Here are the various example of how we can apply the function on different inputs along with the output of the variable after the function is applied. In all these examples I will use htmlentities functions as an example function, as it is easy to identify if the function worked or not. htmlentities function converts all applicable characters to HTML entities. In the following examples if the ‘< ‘ and ‘>’ are converted into ‘<‘ and ‘>’ respectively, that means the function got applied correctly.‘>
Apply htmlentites to the value of a variable:
$var = 'some value is bold'; $var = htmlentities($var); var_dump($var);
string(47) "some value is <strong>bold</strong>"
Apply htmlentities to single dimensional array:
$array = array(); $array['key1'] = 'value 1'; $array['key2'] = 'value 2'; foreach($array as $key => $val) < $array[$key] = htmlentities($val); >var_dump($array);
array(2) < ["key1"]=>string(36) "value <strong>1</strong>" ["key2"]=> string(36) "value <strong>2</strong>" >
Apply htmlentities to two-dimensional array.
Method 1: Use array_map function. This method works only if the array is always a two-dimensional array.
$array = array(); $array['key1']['key2'] = 'value 1'; $array['key1']['key3'] = 'value 2'; foreach($array as &$val) < $val = array_map('htmlentities', $val); >var_dump($array);
array(1) < ["key1"]=>&array(2) < ["key2"]=>string(36) "value <strong>1</strong>" ["key3"]=> string(36) "value <strong>2</strong>" > >
Method 2: There is another way to apply htmlentities to a two-dimensional array without using foreach loop.
$array = array(); $array['key1']['key2'] = 'value 1'; $array['key1']['key3'] = 'value 2'; array_walk_recursive($array, function (&$value) < $value = htmlentities($value); >); var_dump($array);
array(1) < ["key1"]=>array(2) < ["key2"]=>string(36) "value <strong>1</strong>" ["key3"]=> string(36) "value <strong>2</strong>" > >
Apply htmlentites to multidimensional array:
In the method 1 to apply the function to a 2-dimensional array, I have used array_map function. However, array_map does not works with multidimensional arrays. Also, array_map does not works recursively hence we need a loop. Let’s see what we will get if we try to use this method on a multidimensional array:
$array = array(); $array['key1']['key11'] = 'value 11'; $array['key1']['key12'] = 'value 12'; $array['key2']['key13'] = 'value 13'; $array['key2'] = 'value 21'; $array['key3']['key31']['key311'] = 'value 311'; foreach($array as &$val) < $val = array_map('htmlentities', $val); >var_dump($array);
Warning: array_map() [function.array-map]: Argument #2 should be an array in test.php on line 10 Warning: htmlentities() expects parameter 1 to be string, array given in test.php on line 10 array(3) < ["key1"]=>array(2) < ["key11"]=>string(37) "value <strong>11</strong>" ["key12"]=> string(37) "value <strong>12</strong>" > ["key2"]=> NULL ["key3"]=> &array(1) < ["key31"]=>NULL > >
As we saw in the above example, we didn’t get the expected output and also got couple warnings. It didn’t work recursively on all elements of the array. Hence, if you are not sure about the dimensions of the array or if the array has different dimensions at each level then you cannot use array_map. In this case we can use the array_walk_recursive function. The array_walk_recursive function works recursively on the array and hence we don’t need any for loop for it.
$array = array(); $array['key1']['key11'] = 'value 11'; $array['key1']['key12'] = 'value 12'; $array['key2']['key13'] = 'value 13'; $array['key2'] = 'value 21'; $array['key3']['key31']['key311'] = 'value 311'; array_walk_recursive($array, function (&$value) < $value = htmlentities($value); >);
array(3) < ["key1"]=>array(2) < ["key11"]=>string(37) "value <strong>11</strong>" ["key12"]=> string(37) "value <strong>12</strong>" > ["key2"]=> string(37) "value <strong>21</strong>" ["key3"]=> array(1) < ["key31"]=>array(1) < ["key311"]=>string(38) "value <strong>311</strong>" > > >
Note: All the examples above work on PHP 5.3+. They won’t work in PHP versions less than PHP 5.3.0, because they do not support anonymous functions. So to make it work with PHP version < 5.3.0 the above code can be modified as follows:
$array = array(); $array['key1']['key11'] = 'value 11'; $array['key1']['key12'] = 'value 12'; $array['key2']['key13'] = 'value 13'; $array['key2'] = 'value 21'; $array['key3']['key31']['key311'] = 'value 311'; function my_apply_htmlentities(&$value) < $value = htmlentities($value); >array_walk_recursive($array, 'my_apply_htmlentities'); var_dump($array);
array(3) < ["key1"]=>array(2) < ["key11"]=>string(37) "value <strong>11</strong>" ["key12"]=> string(37) "value <strong>12</strong>" > ["key2"]=> string(37) "value <strong>21</strong>" ["key3"]=> array(1) < ["key31"]=>array(1) < ["key311"]=>string(38) "value <strong>311</strong>" > > >
Note: I have tested all the above codes and all codes (except for the last) work on PHP 5.3.13 and the last one works on PHP 5.2.11
Related Links: