Annotation of embedaddon/php/ext/standard/tests/array/array_walk_recursive_variation6.phpt, revision 1.1.1.1
1.1 misho 1: --TEST--
2: Test array_walk_recursive() function : usage variations - 'input' argument as diff. associative arrays
3: --FILE--
4: <?php
5: /* Prototype : bool array_walk_recursive(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: /*
11: * Passing 'input' argument as an associative array
12: * with Numeric & string keys
13: */
14:
15: echo "*** Testing array_walk_recursive() : 'input' as an associative array ***\n";
16:
17: // callback functions
18: /* Prototype : for_numeric( int $value, int $key, int $user_data)
19: * Parameters : $value - value from key/value pair of the array
20: * $key - key from key/value pair of the array
21: * $user_data - data to be added to 'value'
22: * Description : Function adds values with keys & user_data
23: */
24: function for_numeric($value, $key, $user_data)
25: {
26: // dump the input values to see if they are
27: // passed with correct type
28: var_dump($key);
29: var_dump($value);
30: var_dump($user_data);
31: echo "\n"; // new line to separate the output between each element
32: }
33:
34: /* Prototype : for_string( string $value, string $key)
35: * Parameters : $value - values in given input array
36: * $key - keys in given input array
37: * Description : Function appends key to the value
38: */
39: function for_string($value, $key)
40: {
41: // dump the input values to see if they are
42: // passed with correct type
43: var_dump($key);
44: var_dump($value);
45: echo "\n"; // new line to separate the output between each element
46: }
47:
48: /* Prototype : for_mixed( mixed $value, mixed $key)
49: * Parameters : $value - values in given input array
50: * $key - keys in given input array
51: * Description : Function displays each element of an array with keys
52: */
53: function for_mixed($value, $key)
54: {
55: // dump the input values to see if they are
56: // passed with correct type
57: var_dump($key);
58: var_dump($value);
59: echo "\n"; // new line to separate the output between each element
60: }
61:
62: // Numeric keys
63: $input = array( 0 => array(1 => 25, 5 => 12, 0 => -80), 1 => array(-2 => 100, 5 => 30));
64: echo "-- Associative array with numeric keys --\n";
65: var_dump( array_walk_recursive($input, "for_numeric", 10));
66:
67: // String keys
68: $input = array( "a" => "Apple", 'z' => array('b' => 'Bananna', "c" => "carrot"), 'x' => array('o' => "Orange"));
69: echo "-- Associative array with string keys --\n";
70: var_dump( array_walk_recursive($input, "for_string"));
71:
72: // binary key
73: $input = array( b"a" => "Apple", b"b" => "Banana");
74: echo "-- Associative array with binary keys --\n";
75: var_dump( array_walk_recursive($input, "for_string"));
76:
77: // Mixed keys - numeric/string
78: $input = array( 0 => array(0 => 1, 1 => 2), "x" => array("a" => "Apple", "b" => "Banana"), 2 =>3);
79: echo "-- Associative array with numeric/string keys --\n";
80: var_dump( array_walk_recursive($input, "for_mixed"));
81:
82: echo "Done"
83: ?>
84: --EXPECTF--
85: *** Testing array_walk_recursive() : 'input' as an associative array ***
86: -- Associative array with numeric keys --
87: int(1)
88: int(25)
89: int(10)
90:
91: int(5)
92: int(12)
93: int(10)
94:
95: int(0)
96: int(-80)
97: int(10)
98:
99: int(-2)
100: int(100)
101: int(10)
102:
103: int(5)
104: int(30)
105: int(10)
106:
107: bool(true)
108: -- Associative array with string keys --
109: string(1) "a"
110: string(5) "Apple"
111:
112: string(1) "b"
113: string(7) "Bananna"
114:
115: string(1) "c"
116: string(6) "carrot"
117:
118: string(1) "o"
119: string(6) "Orange"
120:
121: bool(true)
122: -- Associative array with binary keys --
123: string(1) "a"
124: string(5) "Apple"
125:
126: string(1) "b"
127: string(6) "Banana"
128:
129: bool(true)
130: -- Associative array with numeric/string keys --
131: int(0)
132: int(1)
133:
134: int(1)
135: int(2)
136:
137: string(1) "a"
138: string(5) "Apple"
139:
140: string(1) "b"
141: string(6) "Banana"
142:
143: int(2)
144: int(3)
145:
146: bool(true)
147: Done
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>