1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
|
--TEST--
Test array_unshift() function : usage variations - different array values for 'array' argument
--FILE--
<?php
/* Prototype : int array_unshift(array $array, mixed $var [, mixed ...])
* Description: Pushes elements onto the beginning of the array
* Source code: ext/standard/array.c
*/
/*
* Testing the behavior of array_unshift() by passing different types of arrays
* to $array argument to which the $var arguments will be prepended
*/
echo "*** Testing array_unshift() : different arrays for \$array argument ***\n";
// initialize $var argument
$var = 10;
// different arrays to be passed to $array argument
$arrays = array (
/*1*/ array(1, 2), // array with default keys and numeric values
array(1.1, 2.2), // array with default keys & float values
array( array(2), array(1)), // sub arrays
array(false,true), // array with default keys and boolean values
array(), // empty array
array(NULL), // array with NULL
array("a","aaaa","b","bbbb","c","ccccc"),
// associative arrays
/*8*/ array(1 => "one", 2 => "two", 3 => "three"), // explicit numeric keys, string values
array("one" => 1, "two" => 2, "three" => 3 ), // string keys & numeric values
array( 1 => 10, 2 => 20, 4 => 40, 3 => 30), // explicit numeric keys and numeric values
array( "one" => "ten", "two" => "twenty", "three" => "thirty"), // string key/value
array("one" => 1, 2 => "two", 4 => "four"), //mixed
// associative array, containing null/empty/boolean values as key/value
/*13*/ array(NULL => "NULL", null => "null", "NULL" => NULL, "null" => null),
array(true => "true", false => "false", "false" => false, "true" => true),
array("" => "emptyd", '' => 'emptys', "emptyd" => "", 'emptys' => ''),
array(1 => '', 2 => "", 3 => NULL, 4 => null, 5 => false, 6 => true),
array('' => 1, "" => 2, NULL => 3, null => 4, false => 5, true => 6),
// array with repetative keys
/*18*/ array("One" => 1, "two" => 2, "One" => 10, "two" => 20, "three" => 3)
);
// loop through the various elements of $arrays to test array_unshift()
$iterator = 1;
foreach($arrays as $array) {
echo "-- Iteration $iterator --\n";
/* with default argument */
// returns element count in the resulting array after arguments are pushed to
// beginning of the given array
$temp_array = $array;
var_dump( array_unshift($temp_array, $var) );
// dump the resulting array
var_dump($temp_array);
/* with optional arguments */
// returns element count in the resulting array after arguments are pushed to
// beginning of the given array
$temp_array = $array;
var_dump( array_unshift($temp_array, $var, "hello", 'world') );
// dump the resulting array
var_dump($temp_array);
$iterator++;
}
echo "Done";
?>
--EXPECTF--
*** Testing array_unshift() : different arrays for $array argument ***
-- Iteration 1 --
int(3)
array(3) {
[0]=>
int(10)
[1]=>
int(1)
[2]=>
int(2)
}
int(5)
array(5) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
[3]=>
int(1)
[4]=>
int(2)
}
-- Iteration 2 --
int(3)
array(3) {
[0]=>
int(10)
[1]=>
float(1.1)
[2]=>
float(2.2)
}
int(5)
array(5) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
[3]=>
float(1.1)
[4]=>
float(2.2)
}
-- Iteration 3 --
int(3)
array(3) {
[0]=>
int(10)
[1]=>
array(1) {
[0]=>
int(2)
}
[2]=>
array(1) {
[0]=>
int(1)
}
}
int(5)
array(5) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
[3]=>
array(1) {
[0]=>
int(2)
}
[4]=>
array(1) {
[0]=>
int(1)
}
}
-- Iteration 4 --
int(3)
array(3) {
[0]=>
int(10)
[1]=>
bool(false)
[2]=>
bool(true)
}
int(5)
array(5) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
[3]=>
bool(false)
[4]=>
bool(true)
}
-- Iteration 5 --
int(1)
array(1) {
[0]=>
int(10)
}
int(3)
array(3) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
}
-- Iteration 6 --
int(2)
array(2) {
[0]=>
int(10)
[1]=>
NULL
}
int(4)
array(4) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
[3]=>
NULL
}
-- Iteration 7 --
int(7)
array(7) {
[0]=>
int(10)
[1]=>
string(1) "a"
[2]=>
string(4) "aaaa"
[3]=>
string(1) "b"
[4]=>
string(4) "bbbb"
[5]=>
string(1) "c"
[6]=>
string(5) "ccccc"
}
int(9)
array(9) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
[3]=>
string(1) "a"
[4]=>
string(4) "aaaa"
[5]=>
string(1) "b"
[6]=>
string(4) "bbbb"
[7]=>
string(1) "c"
[8]=>
string(5) "ccccc"
}
-- Iteration 8 --
int(4)
array(4) {
[0]=>
int(10)
[1]=>
string(3) "one"
[2]=>
string(3) "two"
[3]=>
string(5) "three"
}
int(6)
array(6) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
[3]=>
string(3) "one"
[4]=>
string(3) "two"
[5]=>
string(5) "three"
}
-- Iteration 9 --
int(4)
array(4) {
[0]=>
int(10)
["one"]=>
int(1)
["two"]=>
int(2)
["three"]=>
int(3)
}
int(6)
array(6) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
["one"]=>
int(1)
["two"]=>
int(2)
["three"]=>
int(3)
}
-- Iteration 10 --
int(5)
array(5) {
[0]=>
int(10)
[1]=>
int(10)
[2]=>
int(20)
[3]=>
int(40)
[4]=>
int(30)
}
int(7)
array(7) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
[3]=>
int(10)
[4]=>
int(20)
[5]=>
int(40)
[6]=>
int(30)
}
-- Iteration 11 --
int(4)
array(4) {
[0]=>
int(10)
["one"]=>
string(3) "ten"
["two"]=>
string(6) "twenty"
["three"]=>
string(6) "thirty"
}
int(6)
array(6) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
["one"]=>
string(3) "ten"
["two"]=>
string(6) "twenty"
["three"]=>
string(6) "thirty"
}
-- Iteration 12 --
int(4)
array(4) {
[0]=>
int(10)
["one"]=>
int(1)
[1]=>
string(3) "two"
[2]=>
string(4) "four"
}
int(6)
array(6) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
["one"]=>
int(1)
[3]=>
string(3) "two"
[4]=>
string(4) "four"
}
-- Iteration 13 --
int(4)
array(4) {
[0]=>
int(10)
[""]=>
string(4) "null"
["NULL"]=>
NULL
["null"]=>
NULL
}
int(6)
array(6) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
[""]=>
string(4) "null"
["NULL"]=>
NULL
["null"]=>
NULL
}
-- Iteration 14 --
int(5)
array(5) {
[0]=>
int(10)
[1]=>
string(4) "true"
[2]=>
string(5) "false"
["false"]=>
bool(false)
["true"]=>
bool(true)
}
int(7)
array(7) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
[3]=>
string(4) "true"
[4]=>
string(5) "false"
["false"]=>
bool(false)
["true"]=>
bool(true)
}
-- Iteration 15 --
int(4)
array(4) {
[0]=>
int(10)
[""]=>
string(6) "emptys"
["emptyd"]=>
string(0) ""
["emptys"]=>
string(0) ""
}
int(6)
array(6) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
[""]=>
string(6) "emptys"
["emptyd"]=>
string(0) ""
["emptys"]=>
string(0) ""
}
-- Iteration 16 --
int(7)
array(7) {
[0]=>
int(10)
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
NULL
[4]=>
NULL
[5]=>
bool(false)
[6]=>
bool(true)
}
int(9)
array(9) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
NULL
[6]=>
NULL
[7]=>
bool(false)
[8]=>
bool(true)
}
-- Iteration 17 --
int(4)
array(4) {
[0]=>
int(10)
[""]=>
int(4)
[1]=>
int(5)
[2]=>
int(6)
}
int(6)
array(6) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
[""]=>
int(4)
[3]=>
int(5)
[4]=>
int(6)
}
-- Iteration 18 --
int(4)
array(4) {
[0]=>
int(10)
["One"]=>
int(10)
["two"]=>
int(20)
["three"]=>
int(3)
}
int(6)
array(6) {
[0]=>
int(10)
[1]=>
string(5) "hello"
[2]=>
string(5) "world"
["One"]=>
int(10)
["two"]=>
int(20)
["three"]=>
int(3)
}
Done
|