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 float keys
39/*3*/  array(2.3333 => "float", 44.44 => array(1.1 => "float")),
40       array(1.2 => "f1", 3.33 => "f2", 4.89999922839999 => array(1.1 => "f1"), 3333333.333333 => "f4"),
41
42       // arrays with string keys
43/*5*/  array('\tHello' => array("hello", 'world'), '\v\fworld' => 2.2, 'pen\n' => 111),
44       array("\tHello" => array("hello", 'world'), "\v\fworld" => 2.2, "pen\n" => 111),
45       array("hello", $heredoc => array("heredoc", 'string'), "string"),
46
47       // array with object, unset variable and resource variable
48/*8*/ array(@$unset_var => array("unset"), $fp => 'resource', 11, "hello")
49);
50
51// initialise the second array
52$arr2 = array( 1 => "one", 2, "string" => "hello", "array" => array("a", "b", "c"));
53
54// loop through each sub array of $arrays and check the behavior of array_merge_recursive()
55$iterator = 1;
56foreach($arrays as $arr1) {
57  echo "-- Iteration $iterator --\n";
58
59  // with default argument
60  echo "-- With default argument --\n";
61  var_dump( array_merge_recursive($arr1) );
62
63  // with more arguments
64  echo "-- With more arguments --\n";
65  var_dump( array_merge_recursive($arr1, $arr2) );
66
67  $iterator++;
68}
69
70// close the file resource used
71fclose($fp);
72
73echo "Done";
74?>
75--EXPECTF--
76*** Testing array_merge_recursive() : assoc. array with diff. keys to $arr1 argument ***
77
78Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
79-- Iteration 1 --
80-- With default argument --
81array(2) {
82  [0]=>
83  string(1) "0"
84  [1]=>
85  array(1) {
86    [1]=>
87    string(3) "one"
88  }
89}
90-- With more arguments --
91array(6) {
92  [0]=>
93  string(1) "0"
94  [1]=>
95  array(1) {
96    [1]=>
97    string(3) "one"
98  }
99  [2]=>
100  string(3) "one"
101  [3]=>
102  int(2)
103  ["string"]=>
104  string(5) "hello"
105  ["array"]=>
106  array(3) {
107    [0]=>
108    string(1) "a"
109    [1]=>
110    string(1) "b"
111    [2]=>
112    string(1) "c"
113  }
114}
115-- Iteration 2 --
116-- With default argument --
117array(2) {
118  [0]=>
119  string(1) "1"
120  [1]=>
121  array(4) {
122    [1]=>
123    string(3) "one"
124    [2]=>
125    string(3) "two"
126    [3]=>
127    int(1)
128    [4]=>
129    string(1) "4"
130  }
131}
132-- With more arguments --
133array(6) {
134  [0]=>
135  string(1) "1"
136  [1]=>
137  array(4) {
138    [1]=>
139    string(3) "one"
140    [2]=>
141    string(3) "two"
142    [3]=>
143    int(1)
144    [4]=>
145    string(1) "4"
146  }
147  [2]=>
148  string(3) "one"
149  [3]=>
150  int(2)
151  ["string"]=>
152  string(5) "hello"
153  ["array"]=>
154  array(3) {
155    [0]=>
156    string(1) "a"
157    [1]=>
158    string(1) "b"
159    [2]=>
160    string(1) "c"
161  }
162}
163-- Iteration 3 --
164-- With default argument --
165array(2) {
166  [0]=>
167  string(5) "float"
168  [1]=>
169  array(1) {
170    [1]=>
171    string(5) "float"
172  }
173}
174-- With more arguments --
175array(6) {
176  [0]=>
177  string(5) "float"
178  [1]=>
179  array(1) {
180    [1]=>
181    string(5) "float"
182  }
183  [2]=>
184  string(3) "one"
185  [3]=>
186  int(2)
187  ["string"]=>
188  string(5) "hello"
189  ["array"]=>
190  array(3) {
191    [0]=>
192    string(1) "a"
193    [1]=>
194    string(1) "b"
195    [2]=>
196    string(1) "c"
197  }
198}
199-- Iteration 4 --
200-- With default argument --
201array(4) {
202  [0]=>
203  string(2) "f1"
204  [1]=>
205  string(2) "f2"
206  [2]=>
207  array(1) {
208    [1]=>
209    string(2) "f1"
210  }
211  [3]=>
212  string(2) "f4"
213}
214-- With more arguments --
215array(8) {
216  [0]=>
217  string(2) "f1"
218  [1]=>
219  string(2) "f2"
220  [2]=>
221  array(1) {
222    [1]=>
223    string(2) "f1"
224  }
225  [3]=>
226  string(2) "f4"
227  [4]=>
228  string(3) "one"
229  [5]=>
230  int(2)
231  ["string"]=>
232  string(5) "hello"
233  ["array"]=>
234  array(3) {
235    [0]=>
236    string(1) "a"
237    [1]=>
238    string(1) "b"
239    [2]=>
240    string(1) "c"
241  }
242}
243-- Iteration 5 --
244-- With default argument --
245array(3) {
246  ["\tHello"]=>
247  array(2) {
248    [0]=>
249    string(5) "hello"
250    [1]=>
251    string(5) "world"
252  }
253  ["\v\fworld"]=>
254  float(2.2)
255  ["pen\n"]=>
256  int(111)
257}
258-- With more arguments --
259array(7) {
260  ["\tHello"]=>
261  array(2) {
262    [0]=>
263    string(5) "hello"
264    [1]=>
265    string(5) "world"
266  }
267  ["\v\fworld"]=>
268  float(2.2)
269  ["pen\n"]=>
270  int(111)
271  [0]=>
272  string(3) "one"
273  [1]=>
274  int(2)
275  ["string"]=>
276  string(5) "hello"
277  ["array"]=>
278  array(3) {
279    [0]=>
280    string(1) "a"
281    [1]=>
282    string(1) "b"
283    [2]=>
284    string(1) "c"
285  }
286}
287-- Iteration 6 --
288-- With default argument --
289array(3) {
290  ["	Hello"]=>
291  array(2) {
292    [0]=>
293    string(5) "hello"
294    [1]=>
295    string(5) "world"
296  }
297  ["world"]=>
298  float(2.2)
299  ["pen
300"]=>
301  int(111)
302}
303-- With more arguments --
304array(7) {
305  ["	Hello"]=>
306  array(2) {
307    [0]=>
308    string(5) "hello"
309    [1]=>
310    string(5) "world"
311  }
312  ["world"]=>
313  float(2.2)
314  ["pen
315"]=>
316  int(111)
317  [0]=>
318  string(3) "one"
319  [1]=>
320  int(2)
321  ["string"]=>
322  string(5) "hello"
323  ["array"]=>
324  array(3) {
325    [0]=>
326    string(1) "a"
327    [1]=>
328    string(1) "b"
329    [2]=>
330    string(1) "c"
331  }
332}
333-- Iteration 7 --
334-- With default argument --
335array(3) {
336  [0]=>
337  string(5) "hello"
338  ["Hello world"]=>
339  array(2) {
340    [0]=>
341    string(7) "heredoc"
342    [1]=>
343    string(6) "string"
344  }
345  [1]=>
346  string(6) "string"
347}
348-- With more arguments --
349array(7) {
350  [0]=>
351  string(5) "hello"
352  ["Hello world"]=>
353  array(2) {
354    [0]=>
355    string(7) "heredoc"
356    [1]=>
357    string(6) "string"
358  }
359  [1]=>
360  string(6) "string"
361  [2]=>
362  string(3) "one"
363  [3]=>
364  int(2)
365  ["string"]=>
366  string(5) "hello"
367  ["array"]=>
368  array(3) {
369    [0]=>
370    string(1) "a"
371    [1]=>
372    string(1) "b"
373    [2]=>
374    string(1) "c"
375  }
376}
377-- Iteration 8 --
378-- With default argument --
379array(4) {
380  [""]=>
381  array(1) {
382    [0]=>
383    string(5) "unset"
384  }
385  [0]=>
386  string(8) "resource"
387  [1]=>
388  int(11)
389  [2]=>
390  string(5) "hello"
391}
392-- With more arguments --
393array(8) {
394  [""]=>
395  array(1) {
396    [0]=>
397    string(5) "unset"
398  }
399  [0]=>
400  string(8) "resource"
401  [1]=>
402  int(11)
403  [2]=>
404  string(5) "hello"
405  [3]=>
406  string(3) "one"
407  [4]=>
408  int(2)
409  ["string"]=>
410  string(5) "hello"
411  ["array"]=>
412  array(3) {
413    [0]=>
414    string(1) "a"
415    [1]=>
416    string(1) "b"
417    [2]=>
418    string(1) "c"
419  }
420}
421Done
422