Gold Vision Communications / USA
 GOLDVISION.COM
Serving the Multimedia Community since 1989.
English Site | German Site 

 Web Hosting

 Virtual Dedicated Servers

 Chequeo de dominio

 Haga su pedido

 Estado del pedido

 Support

 Contáctenos

 Download

 Network

 Network Status

 Webdesign / Templates

 Efectuar pago

 Terms & Conditions

 Imprimir

 Notas Legales

 Declaración de Privacidad
  PHP / array_walk
array_walk

array_walk

(PHP 3>= 3.0.3, PHP 4 >= 4.0.0)

array_walk --  Apply a user function to every member of an array

Description

int array_walk ( array array, string func [, mixed userdata])

Applies the user-defined function named by func to each element of array. func will be passed array value as the first parameter and array key as the second parameter. If userdata is supplied, it will be passed as the third parameter to the user function. func must be a user-defined function, and can't be a native PHP function. Thus, you can't use array_walk() straight with str2lower(), you must build a user-defined function with it first, and pass this function as argument.

Note: Instead of a function name, an array containing an object reference and a method name can also be supplied.

If func requires more than two or three arguments, depending on userdata, a warning will be generated each time array_walk() calls func. These warnings may be suppressed by prepending the '@' sign to the array_walk() call, or by using error_reporting().

Note: If func needs to be working with the actual values of the array, specify that the first parameter of func should be passed by reference. Then any changes made to those elements will be made in the array itself.

Modifying the array from inside func may cause unpredictable behavior.

Note: Passing the key and userdata to func was added in 4.0.

In PHP 4 reset() needs to be called as necessary since array_walk() does not reset the array by default.

Users may not change the array itself from the callback function. e.g. Add/delete element, unset the array that array_walk() is applied to. If the array is changed, the behavior of this function is undefined.

Example 1. array_walk() example

$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");

function test_alter (&$item1, $key, $prefix) {
    $item1 = "$prefix: $item1";
}

function test_print ($item2, $key) {
    echo "$key. $item2<br>\n";
}
echo "Before ...:\n";
array_walk ($fruits, 'test_print');
reset ($fruits);
array_walk ($fruits, 'test_alter', 'fruit');
echo "... and after:\n";
reset ($fruits);
array_walk ($fruits, 'test_print');

The printout of the program above will be:
Before ...:
d. lemon
a. orange
b. banana
c. apple
... and after:
d. fruit: lemon
a. fruit: orange
b. fruit: banana
c. fruit: apple

See also each() and list().


© 1998-2007 Gold Vision Communications All Rights Reserved.