1--TEST-- 2Test array_unshift() function : usage variations - assoc. array with diff. keys for 'array' argument 3--FILE-- 4<?php 5/* Prototype : int array_unshift(array $array, mixed $var [, mixed ...]) 6 * Description: Pushes elements onto the beginning of the array 7 * Source code: ext/standard/array.c 8*/ 9 10/* 11 * Testing the functionality of array_unshift() by passing different 12 * associative arrays having different possible keys to $array argument. 13 * The $var argument passed is a fixed value 14*/ 15 16echo "*** Testing array_unshift() : associative array with different keys ***\n"; 17 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// initializing $var argument 39$var = 10; 40 41// different variations of associative arrays to be passed to $array argument 42$arrays = array ( 43 44 // empty array 45/*1*/ array(), 46 47 // arrays with integer keys 48 array(0 => "0"), 49 array(1 => "1"), 50 array(1 => "1", 2 => "2", 3 => "3", 4 => "4"), 51 52 // arrays with float keys 53/*5*/ array(2.3333 => "float"), 54 array(1.2 => "f1", 3.33 => "f2", 55 4.89999922839999 => "f3", 56 33333333.333333 => "f4"), 57 58 // arrays with string keys 59/*7*/ array('\tHello' => 111, 're\td' => "color", 60 '\v\fworld' => 2.2, 'pen\n' => 33), 61 array("\tHello" => 111, "re\td" => "color", 62 "\v\fworld" => 2.2, "pen\n" => 33), 63 array("hello", $heredoc => "string"), // heredoc 64 65 // array with object, unset variable and resource variable 66 array(new classA() => 11, @$unset_var => "hello", $fp => 'resource'), 67 68 // array with mixed keys 69/*11*/ array('hello' => 1, new classA() => 2, "fruit" => 2.2, 70 $fp => 'resource', 133 => "int", 444.432 => "float", 71 @$unset_var => "unset", $heredoc => "heredoc") 72); 73 74// loop through the various elements of $arrays to test array_unshift() 75$iterator = 1; 76foreach($arrays as $array) { 77 echo "-- Iteration $iterator --\n"; 78 79 /* with default argument */ 80 // returns element count in the resulting array after arguments are pushed to 81 // beginning of the given array 82 $temp_array = $array; 83 var_dump( array_unshift($temp_array, $var) ); 84 85 // dump the resulting array 86 var_dump($temp_array); 87 88 /* with optional arguments */ 89 // returns element count in the resulting array after arguments are pushed to 90 // beginning of the given array 91 $temp_array = $array; 92 var_dump( array_unshift($temp_array, $var, "hello", 'world') ); 93 94 // dump the resulting array 95 var_dump($temp_array); 96 $iterator++; 97} 98 99echo "Done"; 100?> 101--EXPECTF-- 102*** Testing array_unshift() : associative array with different keys *** 103 104Warning: Illegal offset type in %s on line %d 105 106Notice: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d 107 108Warning: Illegal offset type in %s on line %d 109 110Notice: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d 111-- Iteration 1 -- 112int(1) 113array(1) { 114 [0]=> 115 int(10) 116} 117int(3) 118array(3) { 119 [0]=> 120 int(10) 121 [1]=> 122 string(5) "hello" 123 [2]=> 124 string(5) "world" 125} 126-- Iteration 2 -- 127int(2) 128array(2) { 129 [0]=> 130 int(10) 131 [1]=> 132 string(1) "0" 133} 134int(4) 135array(4) { 136 [0]=> 137 int(10) 138 [1]=> 139 string(5) "hello" 140 [2]=> 141 string(5) "world" 142 [3]=> 143 string(1) "0" 144} 145-- Iteration 3 -- 146int(2) 147array(2) { 148 [0]=> 149 int(10) 150 [1]=> 151 string(1) "1" 152} 153int(4) 154array(4) { 155 [0]=> 156 int(10) 157 [1]=> 158 string(5) "hello" 159 [2]=> 160 string(5) "world" 161 [3]=> 162 string(1) "1" 163} 164-- Iteration 4 -- 165int(5) 166array(5) { 167 [0]=> 168 int(10) 169 [1]=> 170 string(1) "1" 171 [2]=> 172 string(1) "2" 173 [3]=> 174 string(1) "3" 175 [4]=> 176 string(1) "4" 177} 178int(7) 179array(7) { 180 [0]=> 181 int(10) 182 [1]=> 183 string(5) "hello" 184 [2]=> 185 string(5) "world" 186 [3]=> 187 string(1) "1" 188 [4]=> 189 string(1) "2" 190 [5]=> 191 string(1) "3" 192 [6]=> 193 string(1) "4" 194} 195-- Iteration 5 -- 196int(2) 197array(2) { 198 [0]=> 199 int(10) 200 [1]=> 201 string(5) "float" 202} 203int(4) 204array(4) { 205 [0]=> 206 int(10) 207 [1]=> 208 string(5) "hello" 209 [2]=> 210 string(5) "world" 211 [3]=> 212 string(5) "float" 213} 214-- Iteration 6 -- 215int(5) 216array(5) { 217 [0]=> 218 int(10) 219 [1]=> 220 string(2) "f1" 221 [2]=> 222 string(2) "f2" 223 [3]=> 224 string(2) "f3" 225 [4]=> 226 string(2) "f4" 227} 228int(7) 229array(7) { 230 [0]=> 231 int(10) 232 [1]=> 233 string(5) "hello" 234 [2]=> 235 string(5) "world" 236 [3]=> 237 string(2) "f1" 238 [4]=> 239 string(2) "f2" 240 [5]=> 241 string(2) "f3" 242 [6]=> 243 string(2) "f4" 244} 245-- Iteration 7 -- 246int(5) 247array(5) { 248 [0]=> 249 int(10) 250 ["\tHello"]=> 251 int(111) 252 ["re\td"]=> 253 string(5) "color" 254 ["\v\fworld"]=> 255 float(2.2) 256 ["pen\n"]=> 257 int(33) 258} 259int(7) 260array(7) { 261 [0]=> 262 int(10) 263 [1]=> 264 string(5) "hello" 265 [2]=> 266 string(5) "world" 267 ["\tHello"]=> 268 int(111) 269 ["re\td"]=> 270 string(5) "color" 271 ["\v\fworld"]=> 272 float(2.2) 273 ["pen\n"]=> 274 int(33) 275} 276-- Iteration 8 -- 277int(5) 278array(5) { 279 [0]=> 280 int(10) 281 [" Hello"]=> 282 int(111) 283 ["re d"]=> 284 string(5) "color" 285 ["world"]=> 286 float(2.2) 287 ["pen 288"]=> 289 int(33) 290} 291int(7) 292array(7) { 293 [0]=> 294 int(10) 295 [1]=> 296 string(5) "hello" 297 [2]=> 298 string(5) "world" 299 [" Hello"]=> 300 int(111) 301 ["re d"]=> 302 string(5) "color" 303 ["world"]=> 304 float(2.2) 305 ["pen 306"]=> 307 int(33) 308} 309-- Iteration 9 -- 310int(3) 311array(3) { 312 [0]=> 313 int(10) 314 [1]=> 315 string(5) "hello" 316 ["Hello world"]=> 317 string(6) "string" 318} 319int(5) 320array(5) { 321 [0]=> 322 int(10) 323 [1]=> 324 string(5) "hello" 325 [2]=> 326 string(5) "world" 327 [3]=> 328 string(5) "hello" 329 ["Hello world"]=> 330 string(6) "string" 331} 332-- Iteration 10 -- 333int(3) 334array(3) { 335 [0]=> 336 int(10) 337 [""]=> 338 string(5) "hello" 339 [1]=> 340 string(8) "resource" 341} 342int(5) 343array(5) { 344 [0]=> 345 int(10) 346 [1]=> 347 string(5) "hello" 348 [2]=> 349 string(5) "world" 350 [""]=> 351 string(5) "hello" 352 [3]=> 353 string(8) "resource" 354} 355-- Iteration 11 -- 356int(8) 357array(8) { 358 [0]=> 359 int(10) 360 ["hello"]=> 361 int(1) 362 ["fruit"]=> 363 float(2.2) 364 [1]=> 365 string(8) "resource" 366 [2]=> 367 string(3) "int" 368 [3]=> 369 string(5) "float" 370 [""]=> 371 string(5) "unset" 372 ["Hello world"]=> 373 string(7) "heredoc" 374} 375int(10) 376array(10) { 377 [0]=> 378 int(10) 379 [1]=> 380 string(5) "hello" 381 [2]=> 382 string(5) "world" 383 ["hello"]=> 384 int(1) 385 ["fruit"]=> 386 float(2.2) 387 [3]=> 388 string(8) "resource" 389 [4]=> 390 string(3) "int" 391 [5]=> 392 string(5) "float" 393 [""]=> 394 string(5) "unset" 395 ["Hello world"]=> 396 string(7) "heredoc" 397} 398Done 399