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