1--TEST--
2Test array_combine() function : usage variations - associative array with different values(Bug#43424)
3--FILE--
4<?php
5/* Prototype  : array array_combine(array $keys, array $values)
6 * Description: Creates an array by using the elements of the first parameter as keys
7 *              and the elements of the second as the corresponding values
8 * Source code: ext/standard/array.c
9*/
10
11/*
12* Testing the functionality of array_combine() by passing various
13* associative arrays having different possible values to $keys argument and
14* associative arrays having different possible values to $values argument.
15*/
16
17echo "*** Testing array_combine() : assoc array with diff values to both \$keys and \$values argument ***\n";
18
19// get an unset variable
20$unset_var = 10;
21unset ($unset_var);
22
23// get a resource variable
24$fp = fopen(__FILE__, "r");
25
26// get a class
27class classA
28{
29public function __toString(){
30return "Class A object";
31}
32}
33
34// get a heredoc string
35$heredoc = <<<EOT
36Hello world
37EOT;
38
39// different variations of associative array
40$arrays = array (
41
42       // empty array
43/*1*/  array(),
44
45       // arrays with integer values
46       array('0' => 0),
47       array("1" => 1),
48       array("one" => 1, 'two' => 2, "three" => 3, 4 => 4),
49
50       // arrays with float values
51/*5*/  array("float" => 2.3333),
52       array("f1" => 1.2, 'f2' => 3.33, 3 => 4.89999922839999, 'f4' => 33333333.333),
53
54       // arrays with string values
55/*7*/  array(111 => "\tHello", "red" => "col\tor", 2 => "\v\fworld", 3.3 =>  "pen\n"),
56       array(111 => '\tHello', "red" => 'col\tor', 2 => '\v\fworld', 3.3 =>  'pen\n'),
57       array(1 => "hello", "heredoc" => $heredoc),
58
59       // array with object, unset variable and resource variable
60/*10*/ array(11 => new classA(), "unset" => @$unset_var, "resource" => $fp),
61
62       // array with mixed values
63/*11*/ array(1 => 'hello', 2 => new classA(), 222 => "fruit",
64             'resource' => $fp, "int" => 133, "float" => 444.432,
65             "unset" => @$unset_var, "heredoc" => $heredoc)
66);
67
68
69// loop through each sub-array within $arrays to check the behavior of array_combine()
70$iterator = 1;
71foreach($arrays as $array) {
72  echo "-- Iteration $iterator --\n";
73  var_dump( array_combine($array, $array) );
74  $iterator++;
75}
76
77// close the file resource used
78fclose($fp);
79
80echo "Done";
81?>
82--EXPECTF--
83*** Testing array_combine() : assoc array with diff values to both $keys and $values argument ***
84-- Iteration 1 --
85
86Warning: array_combine(): Both parameters should have at least 1 element in %s on line %d
87bool(false)
88-- Iteration 2 --
89array(1) {
90  [0]=>
91  int(0)
92}
93-- Iteration 3 --
94array(1) {
95  [1]=>
96  int(1)
97}
98-- Iteration 4 --
99array(4) {
100  [1]=>
101  int(1)
102  [2]=>
103  int(2)
104  [3]=>
105  int(3)
106  [4]=>
107  int(4)
108}
109-- Iteration 5 --
110array(1) {
111  ["2.3333"]=>
112  float(2.3333)
113}
114-- Iteration 6 --
115array(4) {
116  ["1.2"]=>
117  float(1.2)
118  ["3.33"]=>
119  float(3.33)
120  ["4.8999992284"]=>
121  float(4.8999992284)
122  ["33333333.333"]=>
123  float(33333333.333)
124}
125-- Iteration 7 --
126array(4) {
127  ["	Hello"]=>
128  string(6) "	Hello"
129  ["col	or"]=>
130  string(6) "col	or"
131  ["world"]=>
132  string(7) "world"
133  ["pen
134"]=>
135  string(4) "pen
136"
137}
138-- Iteration 8 --
139array(4) {
140  ["\tHello"]=>
141  string(7) "\tHello"
142  ["col\tor"]=>
143  string(7) "col\tor"
144  ["\v\fworld"]=>
145  string(9) "\v\fworld"
146  ["pen\n"]=>
147  string(5) "pen\n"
148}
149-- Iteration 9 --
150array(2) {
151  ["hello"]=>
152  string(5) "hello"
153  ["Hello world"]=>
154  string(11) "Hello world"
155}
156-- Iteration 10 --
157array(3) {
158  ["Class A object"]=>
159  object(classA)#%d (0) {
160  }
161  [""]=>
162  NULL
163  ["Resource id #%d"]=>
164  resource(%d) of type (stream)
165}
166-- Iteration 11 --
167array(8) {
168  ["hello"]=>
169  string(5) "hello"
170  ["Class A object"]=>
171  object(classA)#%d (0) {
172  }
173  ["fruit"]=>
174  string(5) "fruit"
175  ["Resource id #%d"]=>
176  resource(%d) of type (stream)
177  [133]=>
178  int(133)
179  ["444.432"]=>
180  float(444.432)
181  [""]=>
182  NULL
183  ["Hello world"]=>
184  string(11) "Hello world"
185}
186Done
187