1--TEST-- 2Test array_merge() function : usage variations - Diff. data types as array keys 3--FILE-- 4<?php 5/* Prototype : array array_merge(array $arr1, array $arr2 [, array $...]) 6 * Description: Merges elements from passed arrays into one array 7 * Source code: ext/standard/array.c 8 */ 9 10/* 11 * Pass an array with different data types as keys to test how array_merge 12 * adds it onto an existing array 13 */ 14 15echo "*** Testing array_merge() : usage variations ***\n"; 16 17// Initialise function arguments not being substituted 18$arr = array ('one' => 1, 'two' => 2); 19 20//get an unset variable 21$unset_var = 10; 22unset ($unset_var); 23 24// heredoc string 25$heredoc = <<<EOT 26hello world 27EOT; 28 29// arrays with keys as different data types to be passed as $input 30$inputs = array( 31 32 // int data 33/*1*/ 'int' => array( 34 0 => 'zero', 35 1 => 'one', 36 12345 => 'positive', 37 -2345 => 'negative', 38 ), 39 40 // float data 41/*2*/ 'float' => array( 42 10.5 => 'positive', 43 -10.5 => 'negative', 44 .5 => 'half', 45 ), 46 47/*3*/ 'extreme floats' => array( 48 12.3456789000e10 => 'large', 49 12.3456789000E-10 => 'small', 50 ), 51 52 // null data 53/*4*/ 'null uppercase' => array( 54 NULL => 'null 1', 55 ), 56 57/*5*/ 'null lowercase' => array( 58 null => 'null 2', 59 ), 60 61 // boolean data 62/*6*/ 'bool lowercase' => array( 63 true => 'lowert', 64 false => 'lowerf', 65 ), 66 67/*7*/ 'bool uppercase' => array( 68 TRUE => 'uppert', 69 FALSE => 'upperf', 70 ), 71 72 // empty data 73/*8*/ 'empty double quotes' => array( 74 "" => 'emptyd', 75 ), 76 77/*9*/ 'empty single quotes' => array( 78 '' => 'emptys', 79 ), 80 81 // string data 82/*10*/ 'string' => array( 83 "stringd" => 'stringd', 84 'strings' => 'strings', 85 $heredoc => 'stringh', 86 ), 87 88 // undefined data 89/*11*/ 'undefined' => array( 90 @$undefined_var => 'undefined', 91 ), 92 93 // unset data 94/*12*/ 'unset' => array( 95 @$unset_var => 'unset', 96 ), 97); 98 99// loop through each element of $inputs to check the behavior of array_merge 100$iterator = 1; 101foreach($inputs as $key => $input) { 102 echo "\n-- Iteration $iterator: $key data --\n"; 103 var_dump( array_merge($input, $arr) ); 104 var_dump( array_merge($arr, $input) ); 105 $iterator++; 106}; 107 108echo "Done"; 109?> 110--EXPECTF-- 111*** Testing array_merge() : usage variations *** 112 113-- Iteration 1: int data -- 114array(6) { 115 [0]=> 116 string(4) "zero" 117 [1]=> 118 string(3) "one" 119 [2]=> 120 string(8) "positive" 121 [3]=> 122 string(8) "negative" 123 ["one"]=> 124 int(1) 125 ["two"]=> 126 int(2) 127} 128array(6) { 129 ["one"]=> 130 int(1) 131 ["two"]=> 132 int(2) 133 [0]=> 134 string(4) "zero" 135 [1]=> 136 string(3) "one" 137 [2]=> 138 string(8) "positive" 139 [3]=> 140 string(8) "negative" 141} 142 143-- Iteration 2: float data -- 144array(5) { 145 [0]=> 146 string(8) "positive" 147 [1]=> 148 string(8) "negative" 149 [2]=> 150 string(4) "half" 151 ["one"]=> 152 int(1) 153 ["two"]=> 154 int(2) 155} 156array(5) { 157 ["one"]=> 158 int(1) 159 ["two"]=> 160 int(2) 161 [0]=> 162 string(8) "positive" 163 [1]=> 164 string(8) "negative" 165 [2]=> 166 string(4) "half" 167} 168 169-- Iteration 3: extreme floats data -- 170array(4) { 171 [0]=> 172 string(5) "large" 173 [1]=> 174 string(5) "small" 175 ["one"]=> 176 int(1) 177 ["two"]=> 178 int(2) 179} 180array(4) { 181 ["one"]=> 182 int(1) 183 ["two"]=> 184 int(2) 185 [0]=> 186 string(5) "large" 187 [1]=> 188 string(5) "small" 189} 190 191-- Iteration 4: null uppercase data -- 192array(3) { 193 [""]=> 194 string(6) "null 1" 195 ["one"]=> 196 int(1) 197 ["two"]=> 198 int(2) 199} 200array(3) { 201 ["one"]=> 202 int(1) 203 ["two"]=> 204 int(2) 205 [""]=> 206 string(6) "null 1" 207} 208 209-- Iteration 5: null lowercase data -- 210array(3) { 211 [""]=> 212 string(6) "null 2" 213 ["one"]=> 214 int(1) 215 ["two"]=> 216 int(2) 217} 218array(3) { 219 ["one"]=> 220 int(1) 221 ["two"]=> 222 int(2) 223 [""]=> 224 string(6) "null 2" 225} 226 227-- Iteration 6: bool lowercase data -- 228array(4) { 229 [0]=> 230 string(6) "lowert" 231 [1]=> 232 string(6) "lowerf" 233 ["one"]=> 234 int(1) 235 ["two"]=> 236 int(2) 237} 238array(4) { 239 ["one"]=> 240 int(1) 241 ["two"]=> 242 int(2) 243 [0]=> 244 string(6) "lowert" 245 [1]=> 246 string(6) "lowerf" 247} 248 249-- Iteration 7: bool uppercase data -- 250array(4) { 251 [0]=> 252 string(6) "uppert" 253 [1]=> 254 string(6) "upperf" 255 ["one"]=> 256 int(1) 257 ["two"]=> 258 int(2) 259} 260array(4) { 261 ["one"]=> 262 int(1) 263 ["two"]=> 264 int(2) 265 [0]=> 266 string(6) "uppert" 267 [1]=> 268 string(6) "upperf" 269} 270 271-- Iteration 8: empty double quotes data -- 272array(3) { 273 [""]=> 274 string(6) "emptyd" 275 ["one"]=> 276 int(1) 277 ["two"]=> 278 int(2) 279} 280array(3) { 281 ["one"]=> 282 int(1) 283 ["two"]=> 284 int(2) 285 [""]=> 286 string(6) "emptyd" 287} 288 289-- Iteration 9: empty single quotes data -- 290array(3) { 291 [""]=> 292 string(6) "emptys" 293 ["one"]=> 294 int(1) 295 ["two"]=> 296 int(2) 297} 298array(3) { 299 ["one"]=> 300 int(1) 301 ["two"]=> 302 int(2) 303 [""]=> 304 string(6) "emptys" 305} 306 307-- Iteration 10: string data -- 308array(5) { 309 ["stringd"]=> 310 string(7) "stringd" 311 ["strings"]=> 312 string(7) "strings" 313 ["hello world"]=> 314 string(7) "stringh" 315 ["one"]=> 316 int(1) 317 ["two"]=> 318 int(2) 319} 320array(5) { 321 ["one"]=> 322 int(1) 323 ["two"]=> 324 int(2) 325 ["stringd"]=> 326 string(7) "stringd" 327 ["strings"]=> 328 string(7) "strings" 329 ["hello world"]=> 330 string(7) "stringh" 331} 332 333-- Iteration 11: undefined data -- 334array(3) { 335 [""]=> 336 string(9) "undefined" 337 ["one"]=> 338 int(1) 339 ["two"]=> 340 int(2) 341} 342array(3) { 343 ["one"]=> 344 int(1) 345 ["two"]=> 346 int(2) 347 [""]=> 348 string(9) "undefined" 349} 350 351-- Iteration 12: unset data -- 352array(3) { 353 [""]=> 354 string(5) "unset" 355 ["one"]=> 356 int(1) 357 ["two"]=> 358 int(2) 359} 360array(3) { 361 ["one"]=> 362 int(1) 363 ["two"]=> 364 int(2) 365 [""]=> 366 string(5) "unset" 367} 368Done