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