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 -- 103 104Warning: array_combine(): Both parameters should have at least 1 element in %s on line %d 105bool(false) 106-- Iteration 2 -- 107array(1) { 108 [0]=> 109 string(1) "0" 110} 111-- Iteration 3 -- 112array(1) { 113 [1]=> 114 string(1) "1" 115} 116-- Iteration 4 -- 117array(4) { 118 [1]=> 119 string(1) "1" 120 [2]=> 121 string(1) "2" 122 [3]=> 123 string(1) "3" 124 [4]=> 125 string(1) "4" 126} 127-- Iteration 5 -- 128array(1) { 129 ["float"]=> 130 string(5) "float" 131} 132-- Iteration 6 -- 133array(4) { 134 ["f1"]=> 135 string(2) "f1" 136 ["f2"]=> 137 string(2) "f2" 138 ["f3"]=> 139 string(2) "f3" 140 ["f4"]=> 141 string(2) "f4" 142} 143-- Iteration 7 -- 144array(4) { 145 [111]=> 146 int(111) 147 ["color"]=> 148 string(5) "color" 149 ["2.2"]=> 150 float(2.2) 151 [33]=> 152 int(33) 153} 154-- Iteration 8 -- 155array(4) { 156 [111]=> 157 int(111) 158 ["color"]=> 159 string(5) "color" 160 ["2.2"]=> 161 float(2.2) 162 [33]=> 163 int(33) 164} 165-- Iteration 9 -- 166array(2) { 167 ["hello"]=> 168 string(5) "hello" 169 ["string"]=> 170 string(6) "string" 171} 172-- Iteration 10 -- 173array(1) { 174 ["hello"]=> 175 string(5) "hello" 176} 177-- Iteration 11 -- 178array(6) { 179 [1]=> 180 int(1) 181 ["2.2"]=> 182 float(2.2) 183 ["int"]=> 184 string(3) "int" 185 ["float"]=> 186 string(5) "float" 187 ["unset"]=> 188 string(5) "unset" 189 ["heredoc"]=> 190 string(7) "heredoc" 191} 192Done 193