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