array_walk
bool array_walk(array array, string func, mixed userdata)
Applies a specified function to each element of an array, along with optional input.
Returns:
TRUE on success; NULL if given invalid arguments
Description:
Traverses the given array and passes each element into the given function in turn. The function given as func
is called once for every element of array
. The function named by func
can accept up to three parameters for each element:
|
param1
|
Key of the current element
|
|
param2
|
Value of the current element
|
|
param3
|
Value of the userdata
argument to array_walk(), if given
|
If the function named by func
accepts more than three parameters, it generates a warning, unless that warning is suppressed either by using the error_reporting() function or by prepending the @ error-suppression operator to the array_walk() call.
array_walk() doesn't directly alter the elements of array
unless you explicitly tell it to do so, by writing the function named by func
to take its first parameter by reference.
Version:
PHP 3 since 3.0.3, PHP 4
Example:
Apply a callback function to each element of an array
function output_element($a) {
print("Element value: $a\n");
}
$arr1 = array("alpha", "baker", "charlie", "delta");
array_walk($arr1, "output_element");
Output:
Element value: alpha
Element value: baker
Element value: charlie
Element value: delta
|