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