1--TEST--
2Test array_merge_recursive() function : usage variations - associative array with different keys
3--FILE--
4<?php
5/*
6 * Testing the functionality of array_merge_recursive() by passing different
7 * associative arrays having different keys to $arr1 argument.
8*/
9
10echo "*** Testing array_merge_recursive() : assoc. array with diff. keys to \$arr1 argument ***\n";
11
12// get an unset variable
13$unset_var = 10;
14unset ($unset_var);
15
16// get a resource variable
17$fp = fopen(__FILE__, "r");
18
19// get a class
20class classA
21{
22  public function __toString(){
23    return "Class A object";
24  }
25}
26
27// get a heredoc string
28$heredoc = <<<EOT
29Hello world
30EOT;
31
32// different associative arrays to be passed to $arr1 argument
33$arrays = array (
34/*1*/  // arrays with integer keys
35       array(0 => "0", 1 => array(1 => "one")),
36       array(1 => "1", 2 => array(1 => "one", 2 => "two", 3 => 1, 4 => "4")),
37
38       // arrays with string keys
39/*5*/  array('\tHello' => array("hello", 'world'), '\v\fworld' => 2.2, 'pen\n' => 111),
40       array("\tHello" => array("hello", 'world'), "\v\fworld" => 2.2, "pen\n" => 111),
41       array("hello", $heredoc => array("heredoc", 'string'), "string"),
42
43       // array with object, unset variable and resource variable
44/*8*/ array(@$unset_var => array("unset"), $fp => 'resource', 11, "hello")
45);
46
47// initialise the second array
48$arr2 = array( 1 => "one", 2, "string" => "hello", "array" => array("a", "b", "c"));
49
50// loop through each sub array of $arrays and check the behavior of array_merge_recursive()
51$iterator = 1;
52foreach($arrays as $arr1) {
53  echo "-- Iteration $iterator --\n";
54
55  // with default argument
56  echo "-- With default argument --\n";
57  var_dump( array_merge_recursive($arr1) );
58
59  // with more arguments
60  echo "-- With more arguments --\n";
61  var_dump( array_merge_recursive($arr1, $arr2) );
62
63  $iterator++;
64}
65
66// close the file resource used
67fclose($fp);
68
69echo "Done";
70?>
71--EXPECTF--
72*** Testing array_merge_recursive() : assoc. array with diff. keys to $arr1 argument ***
73
74Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
75-- Iteration 1 --
76-- With default argument --
77array(2) {
78  [0]=>
79  string(1) "0"
80  [1]=>
81  array(1) {
82    [1]=>
83    string(3) "one"
84  }
85}
86-- With more arguments --
87array(6) {
88  [0]=>
89  string(1) "0"
90  [1]=>
91  array(1) {
92    [1]=>
93    string(3) "one"
94  }
95  [2]=>
96  string(3) "one"
97  [3]=>
98  int(2)
99  ["string"]=>
100  string(5) "hello"
101  ["array"]=>
102  array(3) {
103    [0]=>
104    string(1) "a"
105    [1]=>
106    string(1) "b"
107    [2]=>
108    string(1) "c"
109  }
110}
111-- Iteration 2 --
112-- With default argument --
113array(2) {
114  [0]=>
115  string(1) "1"
116  [1]=>
117  array(4) {
118    [1]=>
119    string(3) "one"
120    [2]=>
121    string(3) "two"
122    [3]=>
123    int(1)
124    [4]=>
125    string(1) "4"
126  }
127}
128-- With more arguments --
129array(6) {
130  [0]=>
131  string(1) "1"
132  [1]=>
133  array(4) {
134    [1]=>
135    string(3) "one"
136    [2]=>
137    string(3) "two"
138    [3]=>
139    int(1)
140    [4]=>
141    string(1) "4"
142  }
143  [2]=>
144  string(3) "one"
145  [3]=>
146  int(2)
147  ["string"]=>
148  string(5) "hello"
149  ["array"]=>
150  array(3) {
151    [0]=>
152    string(1) "a"
153    [1]=>
154    string(1) "b"
155    [2]=>
156    string(1) "c"
157  }
158}
159-- Iteration 3 --
160-- With default argument --
161array(3) {
162  ["\tHello"]=>
163  array(2) {
164    [0]=>
165    string(5) "hello"
166    [1]=>
167    string(5) "world"
168  }
169  ["\v\fworld"]=>
170  float(2.2)
171  ["pen\n"]=>
172  int(111)
173}
174-- With more arguments --
175array(7) {
176  ["\tHello"]=>
177  array(2) {
178    [0]=>
179    string(5) "hello"
180    [1]=>
181    string(5) "world"
182  }
183  ["\v\fworld"]=>
184  float(2.2)
185  ["pen\n"]=>
186  int(111)
187  [0]=>
188  string(3) "one"
189  [1]=>
190  int(2)
191  ["string"]=>
192  string(5) "hello"
193  ["array"]=>
194  array(3) {
195    [0]=>
196    string(1) "a"
197    [1]=>
198    string(1) "b"
199    [2]=>
200    string(1) "c"
201  }
202}
203-- Iteration 4 --
204-- With default argument --
205array(3) {
206  ["	Hello"]=>
207  array(2) {
208    [0]=>
209    string(5) "hello"
210    [1]=>
211    string(5) "world"
212  }
213  ["world"]=>
214  float(2.2)
215  ["pen
216"]=>
217  int(111)
218}
219-- With more arguments --
220array(7) {
221  ["	Hello"]=>
222  array(2) {
223    [0]=>
224    string(5) "hello"
225    [1]=>
226    string(5) "world"
227  }
228  ["world"]=>
229  float(2.2)
230  ["pen
231"]=>
232  int(111)
233  [0]=>
234  string(3) "one"
235  [1]=>
236  int(2)
237  ["string"]=>
238  string(5) "hello"
239  ["array"]=>
240  array(3) {
241    [0]=>
242    string(1) "a"
243    [1]=>
244    string(1) "b"
245    [2]=>
246    string(1) "c"
247  }
248}
249-- Iteration 5 --
250-- With default argument --
251array(3) {
252  [0]=>
253  string(5) "hello"
254  ["Hello world"]=>
255  array(2) {
256    [0]=>
257    string(7) "heredoc"
258    [1]=>
259    string(6) "string"
260  }
261  [1]=>
262  string(6) "string"
263}
264-- With more arguments --
265array(7) {
266  [0]=>
267  string(5) "hello"
268  ["Hello world"]=>
269  array(2) {
270    [0]=>
271    string(7) "heredoc"
272    [1]=>
273    string(6) "string"
274  }
275  [1]=>
276  string(6) "string"
277  [2]=>
278  string(3) "one"
279  [3]=>
280  int(2)
281  ["string"]=>
282  string(5) "hello"
283  ["array"]=>
284  array(3) {
285    [0]=>
286    string(1) "a"
287    [1]=>
288    string(1) "b"
289    [2]=>
290    string(1) "c"
291  }
292}
293-- Iteration 6 --
294-- With default argument --
295array(4) {
296  [""]=>
297  array(1) {
298    [0]=>
299    string(5) "unset"
300  }
301  [0]=>
302  string(8) "resource"
303  [1]=>
304  int(11)
305  [2]=>
306  string(5) "hello"
307}
308-- With more arguments --
309array(8) {
310  [""]=>
311  array(1) {
312    [0]=>
313    string(5) "unset"
314  }
315  [0]=>
316  string(8) "resource"
317  [1]=>
318  int(11)
319  [2]=>
320  string(5) "hello"
321  [3]=>
322  string(3) "one"
323  [4]=>
324  int(2)
325  ["string"]=>
326  string(5) "hello"
327  ["array"]=>
328  array(3) {
329    [0]=>
330    string(1) "a"
331    [1]=>
332    string(1) "b"
333    [2]=>
334    string(1) "c"
335  }
336}
337Done
338