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