PHP » Arrays » array_walk()

Syntax:
bool array_walk(array array, string function [, mixed arg])
array
The array to apply a function to.
function
The name of a function that should be applied to all the elements.
arg
Argument to pass to the function.

Applies a function to an array.

The array_walk() function applies the specified function to every element in an array. The value and key of the element being processed are always passed to the function. If the optional argument parameter is passed to array_walk(), it is also passed to the function.

Examples

Code:
// prints an element
function myPrint($value)
{
   print "$value ";
}

// multiplies an element with the specified factor
function multiply(&$value, $key, $factor)
{
   $value *= $factor;
}

$array1 = array(1, 2, 3);

// multiply the element by three
array_walk($array1, "multiply", 3);
// print the elements
array_walk($array1, "myPrint");

?>
Output:
3 6 9
Explanation:

Two functions are used together with array_walk() to multiply all the elements in an array and print the result.