1--TEST--
2Test array_walk() function : basic functionality - associative array
3--FILE--
4<?php
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
8*/
9
10echo "*** Testing array_walk() : basic functionality ***\n";
11
12// associative array
13$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
14
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
21 */
22function test_alter(&$item, $key, $prefix)
23{
24  // dump the arguments to check that they are passed
25  // with proper type
26  var_dump($item); // value
27  var_dump($key);  // key
28  var_dump($prefix); // additional argument passed to callback function
29  echo "\n"; // new line to separate the output between each element
30}
31
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
36 */
37function test_print($item, $key)
38{
39  // dump the arguments to check that they are passed
40  // with proper type
41  var_dump($item); // value
42  var_dump($key);  // key
43  echo "\n"; // new line to separate the output between each element
44}
45
46echo "-- Using array_walk with default parameters to show array contents --\n";
47var_dump(array_walk($fruits, 'test_print'));
48
49echo "-- Using array_walk with one optional parameter to modify contents --\n";
50var_dump (array_walk($fruits, 'test_alter', 'fruit'));
51
52echo "-- Using array_walk with default parameters to show modified array contents --\n";
53var_dump (array_walk($fruits, 'test_print'));
54
55echo "Done";
56?>
57--EXPECT--
58*** Testing array_walk() : basic functionality ***
59-- Using array_walk with default parameters to show array contents --
60string(5) "lemon"
61string(1) "d"
62
63string(6) "orange"
64string(1) "a"
65
66string(6) "banana"
67string(1) "b"
68
69string(5) "apple"
70string(1) "c"
71
72bool(true)
73-- Using array_walk with one optional parameter to modify contents --
74string(5) "lemon"
75string(1) "d"
76string(5) "fruit"
77
78string(6) "orange"
79string(1) "a"
80string(5) "fruit"
81
82string(6) "banana"
83string(1) "b"
84string(5) "fruit"
85
86string(5) "apple"
87string(1) "c"
88string(5) "fruit"
89
90bool(true)
91-- Using array_walk with default parameters to show modified array contents --
92string(5) "lemon"
93string(1) "d"
94
95string(6) "orange"
96string(1) "a"
97
98string(6) "banana"
99string(1) "b"
100
101string(5) "apple"
102string(1) "c"
103
104bool(true)
105Done
106