Recipe 4.22. Applying a Function to Each Element in an Array


4.22.1. Problem

You want to apply a function or method to each element in an array. This allows you to transform the input data for the entire set all at once.

4.22.2. Solution

Use array_walk( ):

function escape_data(&$value, $key) {     $value = htmlentities($value, ENT_QUOTES); } $names = array('firstname' => "Baba",                'lastname'  => "O'Riley"); array_walk($names, 'escape_data'); foreach ($names as $name) {     print "$name\n"; } Baba O'Riley

For nested data, use array_walk_recursive( ):

function escape_data(&$value, $key) {     $value = htmlentities($value, ENT_QUOTES); } $names = array('firstnames' => array("Baba", "Bill"),                'lastnames'  => array("O'Riley", "O'Reilly")); array_walk_recursive($names, 'escape_data'); foreach ($names as $nametypes) {     foreach ($nametypes as $name) {         print "$name\n";     } } Baba Bill O'Riley O'Reilly

4.22.3. Discussion

It's frequently useful to loop through all the elements of an array. One option is to foreach tHRough the data. However, an alternative choice is the array_walk( ) function.

This function takes an array and the name of a callback function, which is the function that processes the elements of the array. The callback function takes two parameters, a value and a key. It can also take an optional third parameter, which is any additional data you wish to expose within the callback.

Here's an example that ensures all the data in the $names array is properly HTML encoded. The callback function, escape_data( ), takes the array values, passes them to htmlentities( ) to encode the key HTML entities, and assigns the result back to $value:

function escape_data(&$value, $key) {     $value = htmlentities($value, ENT_QUOTES); } $names = array('firstname' => "Baba",                'lastname'  => "O'Riley"); array_walk($names, 'escape_data'); foreach ($names as $name) {     print "$name\n"; } Baba O'Riley

Since array_walk operates in-place instead of returning a modified copy of the array, you must pass in values by reference when you want to modify the elements. In those cases, as in this example, there is an & before the parameter name. However, this is only necessary when you wish to alter the array.

When you have a series of nested arrays, use the array_walk_recursive( ) function:

function escape_data(&$value, $key) {     $value = htmlentities($value, ENT_QUOTES); } $names = array('firstnames' => array("Baba", "Bill"),                'lastnames'  => array("O'Riley", "O'Reilly")); array_walk_recursive($names, 'escape_data'); foreach ($names as $nametypes) {     foreach ($nametypes as $name) {         print "$name\n";     } } Baba Bill O'Riley O'Reilly

The array_walk_recursive( ) function only passes non-array elements to the callback, so you don't need to modify a callback when switching from array_walk( ).

4.22.4. See Also

Documentation on array_walk( ) at http://www.php.net/array-walk, array_walk_recursive( ) at http://www.php.net/array_walk_recursive, and htmlentities( ) at http://www.php.net/htmlentities .




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net