2 Test array_walk() function : basic functionality - associative array
5 /* Prototype : bool array_walk(array $input, string $funcname [, mixed $userdata])
6 * Description: Apply a user function to every member of an array
7 * Source code: ext/standard/array.c
10 echo "*** Testing array_walk() : basic functionality ***\n";
13 $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
15 // User defined callback functions
16 /* Prototype : test_alter(mixed $item, mixed $key, string $prefix)
17 * Parameters : item - value in key/value pair
18 * key - key in key/value pair
19 * prefix - string to be added
20 * Description : alters the array values by appending prefix string
22 function test_alter(&$item, $key, $prefix)
24 // dump the arguments to check that they are passed
26 var_dump($item); // value
27 var_dump($key); // key
28 var_dump($prefix); // additional agument passed to callback function
29 echo "\n"; // new line to separate the output between each element
32 /* Prototype : test_print(mixed $item, mixed $key)
33 * Parameters : item - value in key/value pair
34 * key - key in key/value pair
35 * Description : prints the array values with keys
37 function test_print($item, $key)
39 // dump the arguments to check that they are passed
41 var_dump($item); // value
42 var_dump($key); // key
43 echo "\n"; // new line to separate the output between each element
46 echo "-- Using array_walk with default parameters to show array contents --\n";
47 var_dump(array_walk($fruits, 'test_print'));
49 echo "-- Using array_walk with one optional parameter to modify contents --\n";
50 var_dump (array_walk($fruits, 'test_alter', 'fruit'));
52 echo "-- Using array_walk with default parameters to show modified array contents --\n";
53 var_dump (array_walk($fruits, 'test_print'));
58 *** Testing array_walk() : basic functionality ***
59 -- Using array_walk with default parameters to show array contents --
73 -- Using array_walk with one optional parameter to modify contents --
91 -- Using array_walk with default parameters to show modified array contents --