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
125--EXPECTF--
126*** Testing array_merge() : usage variations ***
127
128-- Iteration 1: int data --
129array(6) {
130  [0]=>
131  int(0)
132  [1]=>
133  int(1)
134  [2]=>
135  int(12345)
136  [3]=>
137  int(-2345)
138  [4]=>
139  int(1)
140  [5]=>
141  int(2)
142}
143array(6) {
144  [0]=>
145  int(1)
146  [1]=>
147  int(2)
148  [2]=>
149  int(0)
150  [3]=>
151  int(1)
152  [4]=>
153  int(12345)
154  [5]=>
155  int(-2345)
156}
157
158-- Iteration 2: float data --
159array(7) {
160  [0]=>
161  float(10.5)
162  [1]=>
163  float(-10.5)
164  [2]=>
165  float(123456789000)
166  [3]=>
167  float(1.23456789E-9)
168  [4]=>
169  float(0.5)
170  [5]=>
171  int(1)
172  [6]=>
173  int(2)
174}
175array(7) {
176  [0]=>
177  int(1)
178  [1]=>
179  int(2)
180  [2]=>
181  float(10.5)
182  [3]=>
183  float(-10.5)
184  [4]=>
185  float(123456789000)
186  [5]=>
187  float(1.23456789E-9)
188  [6]=>
189  float(0.5)
190}
191
192-- Iteration 3: null data --
193array(4) {
194  [0]=>
195  NULL
196  [1]=>
197  NULL
198  [2]=>
199  int(1)
200  [3]=>
201  int(2)
202}
203array(4) {
204  [0]=>
205  int(1)
206  [1]=>
207  int(2)
208  [2]=>
209  NULL
210  [3]=>
211  NULL
212}
213
214-- Iteration 4: bool data --
215array(6) {
216  [0]=>
217  bool(true)
218  [1]=>
219  bool(false)
220  [2]=>
221  bool(true)
222  [3]=>
223  bool(false)
224  [4]=>
225  int(1)
226  [5]=>
227  int(2)
228}
229array(6) {
230  [0]=>
231  int(1)
232  [1]=>
233  int(2)
234  [2]=>
235  bool(true)
236  [3]=>
237  bool(false)
238  [4]=>
239  bool(true)
240  [5]=>
241  bool(false)
242}
243
244-- Iteration 5: empty string data --
245array(4) {
246  [0]=>
247  string(0) ""
248  [1]=>
249  string(0) ""
250  [2]=>
251  int(1)
252  [3]=>
253  int(2)
254}
255array(4) {
256  [0]=>
257  int(1)
258  [1]=>
259  int(2)
260  [2]=>
261  string(0) ""
262  [3]=>
263  string(0) ""
264}
265
266-- Iteration 6: empty array data --
267array(2) {
268  [0]=>
269  int(1)
270  [1]=>
271  int(2)
272}
273array(2) {
274  [0]=>
275  int(1)
276  [1]=>
277  int(2)
278}
279
280-- Iteration 7: string data --
281array(5) {
282  [0]=>
283  string(6) "string"
284  [1]=>
285  string(6) "string"
286  [2]=>
287  string(11) "hello world"
288  [3]=>
289  int(1)
290  [4]=>
291  int(2)
292}
293array(5) {
294  [0]=>
295  int(1)
296  [1]=>
297  int(2)
298  [2]=>
299  string(6) "string"
300  [3]=>
301  string(6) "string"
302  [4]=>
303  string(11) "hello world"
304}
305
306-- Iteration 8: object data --
307array(3) {
308  [0]=>
309  object(classA)#%d (0) {
310  }
311  [1]=>
312  int(1)
313  [2]=>
314  int(2)
315}
316array(3) {
317  [0]=>
318  int(1)
319  [1]=>
320  int(2)
321  [2]=>
322  object(classA)#%d (0) {
323  }
324}
325
326-- Iteration 9: undefined data --
327array(3) {
328  [0]=>
329  NULL
330  [1]=>
331  int(1)
332  [2]=>
333  int(2)
334}
335array(3) {
336  [0]=>
337  int(1)
338  [1]=>
339  int(2)
340  [2]=>
341  NULL
342}
343
344-- Iteration 10: unset data --
345array(3) {
346  [0]=>
347  NULL
348  [1]=>
349  int(1)
350  [2]=>
351  int(2)
352}
353array(3) {
354  [0]=>
355  int(1)
356  [1]=>
357  int(2)
358  [2]=>
359  NULL
360}
361
362-- Iteration 11: resource data --
363array(3) {
364  [0]=>
365  resource(%d) of type (stream)
366  [1]=>
367  int(1)
368  [2]=>
369  int(2)
370}
371array(3) {
372  [0]=>
373  int(1)
374  [1]=>
375  int(2)
376  [2]=>
377  resource(%d) of type (stream)
378}
379Done