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