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 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
|
--TEST--
Test array_reverse() function : usage variations - different array values for 'array' argument
--FILE--
<?php
/* Prototype : array array_reverse(array $array [, bool $preserve_keys])
* Description: Return input as a new array with the order of the entries reversed
* Source code: ext/standard/array.c
*/
/*
* Testing the functionality of array_reverse() by giving
* different array values for $array argument
*/
echo "*** Testing array_reverse() : usage variations ***\n";
//get an unset variable
$unset_var = 10;
unset ($unset_var);
//get a resource variable
$fp = fopen(__FILE__, "r");
//get a class
class classA
{
public function __toString(){
return "Class A object";
}
}
// get a heredoc string
$heredoc = <<<EOT
Hello world
EOT;
$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_reverse()
$iterator = 1;
foreach($arrays as $array) {
echo "-- Iteration $iterator --\n";
// with default argument
echo "- with default argument -\n";
var_dump( array_reverse($array) );
// with all possible arguments
echo "- with \$preserve keys = true -\n";
var_dump( array_reverse($array, true) );
echo "- with \$preserve_keys = false -\n";
var_dump( array_reverse($array, false) );
$iterator++;
};
// close the file resource used
fclose($fp);
echo "Done";
?>
--EXPECTF--
*** Testing array_reverse() : usage variations ***
-- Iteration 1 --
- with default argument -
array(2) {
[0]=>
int(2)
[1]=>
int(1)
}
- with $preserve keys = true -
array(2) {
[1]=>
int(2)
[0]=>
int(1)
}
- with $preserve_keys = false -
array(2) {
[0]=>
int(2)
[1]=>
int(1)
}
-- Iteration 2 --
- with default argument -
array(2) {
[0]=>
float(2.2)
[1]=>
float(1.1)
}
- with $preserve keys = true -
array(2) {
[1]=>
float(2.2)
[0]=>
float(1.1)
}
- with $preserve_keys = false -
array(2) {
[0]=>
float(2.2)
[1]=>
float(1.1)
}
-- Iteration 3 --
- with default argument -
array(2) {
[0]=>
array(1) {
[0]=>
int(1)
}
[1]=>
array(1) {
[0]=>
int(2)
}
}
- with $preserve keys = true -
array(2) {
[1]=>
array(1) {
[0]=>
int(1)
}
[0]=>
array(1) {
[0]=>
int(2)
}
}
- with $preserve_keys = false -
array(2) {
[0]=>
array(1) {
[0]=>
int(1)
}
[1]=>
array(1) {
[0]=>
int(2)
}
}
-- Iteration 4 --
- with default argument -
array(2) {
[0]=>
bool(true)
[1]=>
bool(false)
}
- with $preserve keys = true -
array(2) {
[1]=>
bool(true)
[0]=>
bool(false)
}
- with $preserve_keys = false -
array(2) {
[0]=>
bool(true)
[1]=>
bool(false)
}
-- Iteration 5 --
- with default argument -
array(0) {
}
- with $preserve keys = true -
array(0) {
}
- with $preserve_keys = false -
array(0) {
}
-- Iteration 6 --
- with default argument -
array(1) {
[0]=>
NULL
}
- with $preserve keys = true -
array(1) {
[0]=>
NULL
}
- with $preserve_keys = false -
array(1) {
[0]=>
NULL
}
-- Iteration 7 --
- with default argument -
array(6) {
[0]=>
string(5) "ccccc"
[1]=>
string(1) "c"
[2]=>
string(4) "bbbb"
[3]=>
string(1) "b"
[4]=>
string(4) "aaaa"
[5]=>
string(1) "a"
}
- with $preserve keys = true -
array(6) {
[5]=>
string(5) "ccccc"
[4]=>
string(1) "c"
[3]=>
string(4) "bbbb"
[2]=>
string(1) "b"
[1]=>
string(4) "aaaa"
[0]=>
string(1) "a"
}
- with $preserve_keys = false -
array(6) {
[0]=>
string(5) "ccccc"
[1]=>
string(1) "c"
[2]=>
string(4) "bbbb"
[3]=>
string(1) "b"
[4]=>
string(4) "aaaa"
[5]=>
string(1) "a"
}
-- Iteration 8 --
- with default argument -
array(3) {
[0]=>
string(5) "three"
[1]=>
string(3) "two"
[2]=>
string(3) "one"
}
- with $preserve keys = true -
array(3) {
[3]=>
string(5) "three"
[2]=>
string(3) "two"
[1]=>
string(3) "one"
}
- with $preserve_keys = false -
array(3) {
[0]=>
string(5) "three"
[1]=>
string(3) "two"
[2]=>
string(3) "one"
}
-- Iteration 9 --
- with default argument -
array(3) {
["three"]=>
int(3)
["two"]=>
int(2)
["one"]=>
int(1)
}
- with $preserve keys = true -
array(3) {
["three"]=>
int(3)
["two"]=>
int(2)
["one"]=>
int(1)
}
- with $preserve_keys = false -
array(3) {
["three"]=>
int(3)
["two"]=>
int(2)
["one"]=>
int(1)
}
-- Iteration 10 --
- with default argument -
array(4) {
[0]=>
int(30)
[1]=>
int(40)
[2]=>
int(20)
[3]=>
int(10)
}
- with $preserve keys = true -
array(4) {
[3]=>
int(30)
[4]=>
int(40)
[2]=>
int(20)
[1]=>
int(10)
}
- with $preserve_keys = false -
array(4) {
[0]=>
int(30)
[1]=>
int(40)
[2]=>
int(20)
[3]=>
int(10)
}
-- Iteration 11 --
- with default argument -
array(3) {
["three"]=>
string(6) "thirty"
["two"]=>
string(6) "twenty"
["one"]=>
string(3) "ten"
}
- with $preserve keys = true -
array(3) {
["three"]=>
string(6) "thirty"
["two"]=>
string(6) "twenty"
["one"]=>
string(3) "ten"
}
- with $preserve_keys = false -
array(3) {
["three"]=>
string(6) "thirty"
["two"]=>
string(6) "twenty"
["one"]=>
string(3) "ten"
}
-- Iteration 12 --
- with default argument -
array(3) {
[0]=>
string(4) "four"
[1]=>
string(3) "two"
["one"]=>
int(1)
}
- with $preserve keys = true -
array(3) {
[4]=>
string(4) "four"
[2]=>
string(3) "two"
["one"]=>
int(1)
}
- with $preserve_keys = false -
array(3) {
[0]=>
string(4) "four"
[1]=>
string(3) "two"
["one"]=>
int(1)
}
-- Iteration 13 --
- with default argument -
array(3) {
["null"]=>
NULL
["NULL"]=>
NULL
[""]=>
string(4) "null"
}
- with $preserve keys = true -
array(3) {
["null"]=>
NULL
["NULL"]=>
NULL
[""]=>
string(4) "null"
}
- with $preserve_keys = false -
array(3) {
["null"]=>
NULL
["NULL"]=>
NULL
[""]=>
string(4) "null"
}
-- Iteration 14 --
- with default argument -
array(4) {
["true"]=>
bool(true)
["false"]=>
bool(false)
[0]=>
string(5) "false"
[1]=>
string(4) "true"
}
- with $preserve keys = true -
array(4) {
["true"]=>
bool(true)
["false"]=>
bool(false)
[0]=>
string(5) "false"
[1]=>
string(4) "true"
}
- with $preserve_keys = false -
array(4) {
["true"]=>
bool(true)
["false"]=>
bool(false)
[0]=>
string(5) "false"
[1]=>
string(4) "true"
}
-- Iteration 15 --
- with default argument -
array(3) {
["emptys"]=>
string(0) ""
["emptyd"]=>
string(0) ""
[""]=>
string(6) "emptys"
}
- with $preserve keys = true -
array(3) {
["emptys"]=>
string(0) ""
["emptyd"]=>
string(0) ""
[""]=>
string(6) "emptys"
}
- with $preserve_keys = false -
array(3) {
["emptys"]=>
string(0) ""
["emptyd"]=>
string(0) ""
[""]=>
string(6) "emptys"
}
-- Iteration 16 --
- with default argument -
array(6) {
[0]=>
bool(true)
[1]=>
bool(false)
[2]=>
NULL
[3]=>
NULL
[4]=>
string(0) ""
[5]=>
string(0) ""
}
- with $preserve keys = true -
array(6) {
[6]=>
bool(true)
[5]=>
bool(false)
[4]=>
NULL
[3]=>
NULL
[2]=>
string(0) ""
[1]=>
string(0) ""
}
- with $preserve_keys = false -
array(6) {
[0]=>
bool(true)
[1]=>
bool(false)
[2]=>
NULL
[3]=>
NULL
[4]=>
string(0) ""
[5]=>
string(0) ""
}
-- Iteration 17 --
- with default argument -
array(3) {
[0]=>
int(6)
[1]=>
int(5)
[""]=>
int(4)
}
- with $preserve keys = true -
array(3) {
[1]=>
int(6)
[0]=>
int(5)
[""]=>
int(4)
}
- with $preserve_keys = false -
array(3) {
[0]=>
int(6)
[1]=>
int(5)
[""]=>
int(4)
}
-- Iteration 18 --
- with default argument -
array(3) {
["three"]=>
int(3)
["two"]=>
int(20)
["One"]=>
int(10)
}
- with $preserve keys = true -
array(3) {
["three"]=>
int(3)
["two"]=>
int(20)
["One"]=>
int(10)
}
- with $preserve_keys = false -
array(3) {
["three"]=>
int(3)
["two"]=>
int(20)
["One"]=>
int(10)
}
Done
|