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