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