1--TEST--
2Test array_merge() function
3--INI--
4precision=14
5--FILE--
6<?php
7/* Prototype: array array_merge(array $array1 [, array $array2 [, array $...]]);
8   Description: Merge one or more arrays
9*/
10
11echo "\n*** Testing array_merge() basic functionality ***";
12$begin_array = array(
13  array(),
14  array( 1 => "string"),
15  array( "" => "string"),
16  array( -2.44444 => 12),
17  array( "a" => 1, "b" => -2.344, "b" => "string", "c" => NULL,	"d" => -2.344),
18  array( 4 => 1, 3 => -2.344, "3" => "string", "2" => NULL,1 => -2.344),
19  array( NULL, 1.23 => "Hi", "string" => "hello",
20  array("" => "World", "-2.34" => "a", "0" => "b"))
21);
22
23$end_array   = array(
24  array(),
25  array( 1 => "string"),
26  array( "" => "string"),
27  array( -2.44444 => 12),
28  array( "a" => 1, "b" => -2.344, "b" => "string", "c" => NULL, "d" => -2.344),
29  array( 4 => 1, 3 => -2.344, "3" => "string", "2" => NULL, 1=> -2.344),
30  array( NULL, 1.23 => "Hi", "string" => "hello",
31         array("" => "World", "-2.34" => "a", "0" => "b"))
32);
33
34/* loop through to merge two arrays */
35$count_outer = 0;
36foreach($begin_array as $first) {
37  echo "\n\n--- Iteration $count_outer ---";
38  $count_inner = 0;
39  foreach($end_array as $second) {
40    echo "\n-- Inner iteration $count_inner of Iteration $count_outer --\n";
41    $result = array_merge($first, $second);
42    print_r($result);
43    $count_inner++;
44  }
45  $count_outer++;
46}
47
48
49echo "\n*** Testing array_merge() with three or more arrays ***\n";
50var_dump( array_merge( $end_array[0],
51                       $end_array[5],
52                       $end_array[4],
53                       $end_array[6]
54                     )
55        );
56
57var_dump( array_merge( $end_array[0],
58                       $end_array[5],
59                       array("array on fly"),
60                       array("nullarray" => array())
61                     )
62        );
63
64
65echo "\n*** Testing single array argument ***\n";
66/* Empty array */
67var_dump(array_merge(array()));
68
69/* associative array with string keys, which will not be re-indexed */
70var_dump(array_merge($begin_array[4]));
71
72/* associative array with numeric keys, which will be re-indexed */
73var_dump(array_merge($begin_array[5]));
74
75/* associative array with mixed keys and sub-array as element */
76var_dump(array_merge($begin_array[6]));
77
78echo "\n*** Testing array_merge() with typecasting non-array to array ***\n";
79var_dump(array_merge($begin_array[4], (array)"type1", (array)10, (array)12.34));
80
81echo "\n*** Testing error conditions ***";
82/* Invalid arguments */
83var_dump(array_merge());
84var_dump(array_merge(100, 200));
85var_dump(array_merge($begin_array[0], $begin_array[1], 100));
86var_dump(array_merge($begin_array[0], $begin_array[1], $arr4));
87
88echo "Done\n";
89?>
90--EXPECTF--
91*** Testing array_merge() basic functionality ***
92
93--- Iteration 0 ---
94-- Inner iteration 0 of Iteration 0 --
95Array
96(
97)
98
99-- Inner iteration 1 of Iteration 0 --
100Array
101(
102    [0] => string
103)
104
105-- Inner iteration 2 of Iteration 0 --
106Array
107(
108    [] => string
109)
110
111-- Inner iteration 3 of Iteration 0 --
112Array
113(
114    [0] => 12
115)
116
117-- Inner iteration 4 of Iteration 0 --
118Array
119(
120    [a] => 1
121    [b] => string
122    [c] =>
123    [d] => -2.344
124)
125
126-- Inner iteration 5 of Iteration 0 --
127Array
128(
129    [0] => 1
130    [1] => string
131    [2] =>
132    [3] => -2.344
133)
134
135-- Inner iteration 6 of Iteration 0 --
136Array
137(
138    [0] =>
139    [1] => Hi
140    [string] => hello
141    [2] => Array
142        (
143            [] => World
144            [-2.34] => a
145            [0] => b
146        )
147
148)
149
150
151--- Iteration 1 ---
152-- Inner iteration 0 of Iteration 1 --
153Array
154(
155    [0] => string
156)
157
158-- Inner iteration 1 of Iteration 1 --
159Array
160(
161    [0] => string
162    [1] => string
163)
164
165-- Inner iteration 2 of Iteration 1 --
166Array
167(
168    [0] => string
169    [] => string
170)
171
172-- Inner iteration 3 of Iteration 1 --
173Array
174(
175    [0] => string
176    [1] => 12
177)
178
179-- Inner iteration 4 of Iteration 1 --
180Array
181(
182    [0] => string
183    [a] => 1
184    [b] => string
185    [c] =>
186    [d] => -2.344
187)
188
189-- Inner iteration 5 of Iteration 1 --
190Array
191(
192    [0] => string
193    [1] => 1
194    [2] => string
195    [3] =>
196    [4] => -2.344
197)
198
199-- Inner iteration 6 of Iteration 1 --
200Array
201(
202    [0] => string
203    [1] =>
204    [2] => Hi
205    [string] => hello
206    [3] => Array
207        (
208            [] => World
209            [-2.34] => a
210            [0] => b
211        )
212
213)
214
215
216--- Iteration 2 ---
217-- Inner iteration 0 of Iteration 2 --
218Array
219(
220    [] => string
221)
222
223-- Inner iteration 1 of Iteration 2 --
224Array
225(
226    [] => string
227    [0] => string
228)
229
230-- Inner iteration 2 of Iteration 2 --
231Array
232(
233    [] => string
234)
235
236-- Inner iteration 3 of Iteration 2 --
237Array
238(
239    [] => string
240    [0] => 12
241)
242
243-- Inner iteration 4 of Iteration 2 --
244Array
245(
246    [] => string
247    [a] => 1
248    [b] => string
249    [c] =>
250    [d] => -2.344
251)
252
253-- Inner iteration 5 of Iteration 2 --
254Array
255(
256    [] => string
257    [0] => 1
258    [1] => string
259    [2] =>
260    [3] => -2.344
261)
262
263-- Inner iteration 6 of Iteration 2 --
264Array
265(
266    [] => string
267    [0] =>
268    [1] => Hi
269    [string] => hello
270    [2] => Array
271        (
272            [] => World
273            [-2.34] => a
274            [0] => b
275        )
276
277)
278
279
280--- Iteration 3 ---
281-- Inner iteration 0 of Iteration 3 --
282Array
283(
284    [0] => 12
285)
286
287-- Inner iteration 1 of Iteration 3 --
288Array
289(
290    [0] => 12
291    [1] => string
292)
293
294-- Inner iteration 2 of Iteration 3 --
295Array
296(
297    [0] => 12
298    [] => string
299)
300
301-- Inner iteration 3 of Iteration 3 --
302Array
303(
304    [0] => 12
305    [1] => 12
306)
307
308-- Inner iteration 4 of Iteration 3 --
309Array
310(
311    [0] => 12
312    [a] => 1
313    [b] => string
314    [c] =>
315    [d] => -2.344
316)
317
318-- Inner iteration 5 of Iteration 3 --
319Array
320(
321    [0] => 12
322    [1] => 1
323    [2] => string
324    [3] =>
325    [4] => -2.344
326)
327
328-- Inner iteration 6 of Iteration 3 --
329Array
330(
331    [0] => 12
332    [1] =>
333    [2] => Hi
334    [string] => hello
335    [3] => Array
336        (
337            [] => World
338            [-2.34] => a
339            [0] => b
340        )
341
342)
343
344
345--- Iteration 4 ---
346-- Inner iteration 0 of Iteration 4 --
347Array
348(
349    [a] => 1
350    [b] => string
351    [c] =>
352    [d] => -2.344
353)
354
355-- Inner iteration 1 of Iteration 4 --
356Array
357(
358    [a] => 1
359    [b] => string
360    [c] =>
361    [d] => -2.344
362    [0] => string
363)
364
365-- Inner iteration 2 of Iteration 4 --
366Array
367(
368    [a] => 1
369    [b] => string
370    [c] =>
371    [d] => -2.344
372    [] => string
373)
374
375-- Inner iteration 3 of Iteration 4 --
376Array
377(
378    [a] => 1
379    [b] => string
380    [c] =>
381    [d] => -2.344
382    [0] => 12
383)
384
385-- Inner iteration 4 of Iteration 4 --
386Array
387(
388    [a] => 1
389    [b] => string
390    [c] =>
391    [d] => -2.344
392)
393
394-- Inner iteration 5 of Iteration 4 --
395Array
396(
397    [a] => 1
398    [b] => string
399    [c] =>
400    [d] => -2.344
401    [0] => 1
402    [1] => string
403    [2] =>
404    [3] => -2.344
405)
406
407-- Inner iteration 6 of Iteration 4 --
408Array
409(
410    [a] => 1
411    [b] => string
412    [c] =>
413    [d] => -2.344
414    [0] =>
415    [1] => Hi
416    [string] => hello
417    [2] => Array
418        (
419            [] => World
420            [-2.34] => a
421            [0] => b
422        )
423
424)
425
426
427--- Iteration 5 ---
428-- Inner iteration 0 of Iteration 5 --
429Array
430(
431    [0] => 1
432    [1] => string
433    [2] =>
434    [3] => -2.344
435)
436
437-- Inner iteration 1 of Iteration 5 --
438Array
439(
440    [0] => 1
441    [1] => string
442    [2] =>
443    [3] => -2.344
444    [4] => string
445)
446
447-- Inner iteration 2 of Iteration 5 --
448Array
449(
450    [0] => 1
451    [1] => string
452    [2] =>
453    [3] => -2.344
454    [] => string
455)
456
457-- Inner iteration 3 of Iteration 5 --
458Array
459(
460    [0] => 1
461    [1] => string
462    [2] =>
463    [3] => -2.344
464    [4] => 12
465)
466
467-- Inner iteration 4 of Iteration 5 --
468Array
469(
470    [0] => 1
471    [1] => string
472    [2] =>
473    [3] => -2.344
474    [a] => 1
475    [b] => string
476    [c] =>
477    [d] => -2.344
478)
479
480-- Inner iteration 5 of Iteration 5 --
481Array
482(
483    [0] => 1
484    [1] => string
485    [2] =>
486    [3] => -2.344
487    [4] => 1
488    [5] => string
489    [6] =>
490    [7] => -2.344
491)
492
493-- Inner iteration 6 of Iteration 5 --
494Array
495(
496    [0] => 1
497    [1] => string
498    [2] =>
499    [3] => -2.344
500    [4] =>
501    [5] => Hi
502    [string] => hello
503    [6] => Array
504        (
505            [] => World
506            [-2.34] => a
507            [0] => b
508        )
509
510)
511
512
513--- Iteration 6 ---
514-- Inner iteration 0 of Iteration 6 --
515Array
516(
517    [0] =>
518    [1] => Hi
519    [string] => hello
520    [2] => Array
521        (
522            [] => World
523            [-2.34] => a
524            [0] => b
525        )
526
527)
528
529-- Inner iteration 1 of Iteration 6 --
530Array
531(
532    [0] =>
533    [1] => Hi
534    [string] => hello
535    [2] => Array
536        (
537            [] => World
538            [-2.34] => a
539            [0] => b
540        )
541
542    [3] => string
543)
544
545-- Inner iteration 2 of Iteration 6 --
546Array
547(
548    [0] =>
549    [1] => Hi
550    [string] => hello
551    [2] => Array
552        (
553            [] => World
554            [-2.34] => a
555            [0] => b
556        )
557
558    [] => string
559)
560
561-- Inner iteration 3 of Iteration 6 --
562Array
563(
564    [0] =>
565    [1] => Hi
566    [string] => hello
567    [2] => Array
568        (
569            [] => World
570            [-2.34] => a
571            [0] => b
572        )
573
574    [3] => 12
575)
576
577-- Inner iteration 4 of Iteration 6 --
578Array
579(
580    [0] =>
581    [1] => Hi
582    [string] => hello
583    [2] => Array
584        (
585            [] => World
586            [-2.34] => a
587            [0] => b
588        )
589
590    [a] => 1
591    [b] => string
592    [c] =>
593    [d] => -2.344
594)
595
596-- Inner iteration 5 of Iteration 6 --
597Array
598(
599    [0] =>
600    [1] => Hi
601    [string] => hello
602    [2] => Array
603        (
604            [] => World
605            [-2.34] => a
606            [0] => b
607        )
608
609    [3] => 1
610    [4] => string
611    [5] =>
612    [6] => -2.344
613)
614
615-- Inner iteration 6 of Iteration 6 --
616Array
617(
618    [0] =>
619    [1] => Hi
620    [string] => hello
621    [2] => Array
622        (
623            [] => World
624            [-2.34] => a
625            [0] => b
626        )
627
628    [3] =>
629    [4] => Hi
630    [5] => Array
631        (
632            [] => World
633            [-2.34] => a
634            [0] => b
635        )
636
637)
638
639*** Testing array_merge() with three or more arrays ***
640array(12) {
641  [0]=>
642  int(1)
643  [1]=>
644  string(6) "string"
645  [2]=>
646  NULL
647  [3]=>
648  float(-2.344)
649  ["a"]=>
650  int(1)
651  ["b"]=>
652  string(6) "string"
653  ["c"]=>
654  NULL
655  ["d"]=>
656  float(-2.344)
657  [4]=>
658  NULL
659  [5]=>
660  string(2) "Hi"
661  ["string"]=>
662  string(5) "hello"
663  [6]=>
664  array(3) {
665    [""]=>
666    string(5) "World"
667    ["-2.34"]=>
668    string(1) "a"
669    [0]=>
670    string(1) "b"
671  }
672}
673array(6) {
674  [0]=>
675  int(1)
676  [1]=>
677  string(6) "string"
678  [2]=>
679  NULL
680  [3]=>
681  float(-2.344)
682  [4]=>
683  string(12) "array on fly"
684  ["nullarray"]=>
685  array(0) {
686  }
687}
688
689*** Testing single array argument ***
690array(0) {
691}
692array(4) {
693  ["a"]=>
694  int(1)
695  ["b"]=>
696  string(6) "string"
697  ["c"]=>
698  NULL
699  ["d"]=>
700  float(-2.344)
701}
702array(4) {
703  [0]=>
704  int(1)
705  [1]=>
706  string(6) "string"
707  [2]=>
708  NULL
709  [3]=>
710  float(-2.344)
711}
712array(4) {
713  [0]=>
714  NULL
715  [1]=>
716  string(2) "Hi"
717  ["string"]=>
718  string(5) "hello"
719  [2]=>
720  array(3) {
721    [""]=>
722    string(5) "World"
723    ["-2.34"]=>
724    string(1) "a"
725    [0]=>
726    string(1) "b"
727  }
728}
729
730*** Testing array_merge() with typecasting non-array to array ***
731array(7) {
732  ["a"]=>
733  int(1)
734  ["b"]=>
735  string(6) "string"
736  ["c"]=>
737  NULL
738  ["d"]=>
739  float(-2.344)
740  [0]=>
741  string(5) "type1"
742  [1]=>
743  int(10)
744  [2]=>
745  float(12.34)
746}
747
748*** Testing error conditions ***
749Warning: array_merge() expects at least 1 parameter, 0 given in %s on line %d
750NULL
751
752Warning: array_merge(): Argument #1 is not an array in %s on line %d
753NULL
754
755Warning: array_merge(): Argument #3 is not an array in %s on line %d
756NULL
757
758Notice: Undefined variable: arr4 in %s on line %d
759
760Warning: array_merge(): Argument #3 is not an array in %s on line %d
761NULL
762Done
763