Annotation of embedaddon/php/ext/standard/tests/array/array_walk_basic2.phpt, revision 1.1.1.1
1.1 misho 1: --TEST--
2: Test 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:
10: echo "*** 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: */
22: function 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 agument 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: */
37: function 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:
46: echo "-- Using array_walk with default parameters to show array contents --\n";
47: var_dump(array_walk($fruits, 'test_print'));
48:
49: echo "-- Using array_walk with one optional parameter to modify contents --\n";
50: var_dump (array_walk($fruits, 'test_alter', 'fruit'));
51:
52: echo "-- Using array_walk with default parameters to show modified array contents --\n";
53: var_dump (array_walk($fruits, 'test_print'));
54:
55: echo "Done";
56: ?>
57: --EXPECT--
58: *** Testing array_walk() : basic functionality ***
59: -- Using array_walk with default parameters to show array contents --
60: string(5) "lemon"
61: string(1) "d"
62:
63: string(6) "orange"
64: string(1) "a"
65:
66: string(6) "banana"
67: string(1) "b"
68:
69: string(5) "apple"
70: string(1) "c"
71:
72: bool(true)
73: -- Using array_walk with one optional parameter to modify contents --
74: string(5) "lemon"
75: string(1) "d"
76: string(5) "fruit"
77:
78: string(6) "orange"
79: string(1) "a"
80: string(5) "fruit"
81:
82: string(6) "banana"
83: string(1) "b"
84: string(5) "fruit"
85:
86: string(5) "apple"
87: string(1) "c"
88: string(5) "fruit"
89:
90: bool(true)
91: -- Using array_walk with default parameters to show modified array contents --
92: string(5) "lemon"
93: string(1) "d"
94:
95: string(6) "orange"
96: string(1) "a"
97:
98: string(6) "banana"
99: string(1) "b"
100:
101: string(5) "apple"
102: string(1) "c"
103:
104: bool(true)
105: Done
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>