1--TEST-- 2Test array_merge() function 3--INI-- 4precision=14 5--FILE-- 6<?php 7/* Prototype: array array_merge(array $array1 [, array $array2 [, array $...]]); 8 Description: Merge one or more arrays 9*/ 10 11echo "\n*** Testing array_merge() basic functionality ***"; 12$begin_array = array( 13 array(), 14 array( 1 => "string"), 15 array( "" => "string"), 16 array( -2.44444 => 12), 17 array( "a" => 1, "b" => -2.344, "b" => "string", "c" => NULL, "d" => -2.344), 18 array( 4 => 1, 3 => -2.344, "3" => "string", "2" => NULL,1 => -2.344), 19 array( NULL, 1.23 => "Hi", "string" => "hello", 20 array("" => "World", "-2.34" => "a", "0" => "b")) 21); 22 23$end_array = array( 24 array(), 25 array( 1 => "string"), 26 array( "" => "string"), 27 array( -2.44444 => 12), 28 array( "a" => 1, "b" => -2.344, "b" => "string", "c" => NULL, "d" => -2.344), 29 array( 4 => 1, 3 => -2.344, "3" => "string", "2" => NULL, 1=> -2.344), 30 array( NULL, 1.23 => "Hi", "string" => "hello", 31 array("" => "World", "-2.34" => "a", "0" => "b")) 32); 33 34/* loop through to merge two arrays */ 35$count_outer = 0; 36foreach($begin_array as $first) { 37 echo "\n\n--- Iteration $count_outer ---"; 38 $count_inner = 0; 39 foreach($end_array as $second) { 40 echo "\n-- Inner iteration $count_inner of Iteration $count_outer --\n"; 41 $result = array_merge($first, $second); 42 print_r($result); 43 $count_inner++; 44 } 45 $count_outer++; 46} 47 48 49echo "\n*** Testing array_merge() with three or more arrays ***\n"; 50var_dump( array_merge( $end_array[0], 51 $end_array[5], 52 $end_array[4], 53 $end_array[6] 54 ) 55 ); 56 57var_dump( array_merge( $end_array[0], 58 $end_array[5], 59 array("array on fly"), 60 array("nullarray" => array()) 61 ) 62 ); 63 64 65echo "\n*** Testing single array argument ***\n"; 66/* Empty array */ 67var_dump(array_merge(array())); 68 69/* associative array with string keys, which will not be re-indexed */ 70var_dump(array_merge($begin_array[4])); 71 72/* associative array with numeric keys, which will be re-indexed */ 73var_dump(array_merge($begin_array[5])); 74 75/* associative array with mixed keys and sub-array as element */ 76var_dump(array_merge($begin_array[6])); 77 78echo "\n*** Testing array_merge() with typecasting non-array to array ***\n"; 79var_dump(array_merge($begin_array[4], (array)"type1", (array)10, (array)12.34)); 80 81echo "\n*** Testing error conditions ***"; 82/* Invalid arguments */ 83var_dump(array_merge(100, 200)); 84var_dump(array_merge($begin_array[0], $begin_array[1], 100)); 85var_dump(array_merge($begin_array[0], $begin_array[1], $arr4)); 86 87echo "\n*** Testing array_merge without any arguments ***\n"; 88var_dump(array_merge()); 89 90echo "Done\n"; 91?> 92--EXPECTF-- 93*** Testing array_merge() basic functionality *** 94 95--- Iteration 0 --- 96-- Inner iteration 0 of Iteration 0 -- 97Array 98( 99) 100 101-- Inner iteration 1 of Iteration 0 -- 102Array 103( 104 [0] => string 105) 106 107-- Inner iteration 2 of Iteration 0 -- 108Array 109( 110 [] => string 111) 112 113-- Inner iteration 3 of Iteration 0 -- 114Array 115( 116 [0] => 12 117) 118 119-- Inner iteration 4 of Iteration 0 -- 120Array 121( 122 [a] => 1 123 [b] => string 124 [c] => 125 [d] => -2.344 126) 127 128-- Inner iteration 5 of Iteration 0 -- 129Array 130( 131 [0] => 1 132 [1] => string 133 [2] => 134 [3] => -2.344 135) 136 137-- Inner iteration 6 of Iteration 0 -- 138Array 139( 140 [0] => 141 [1] => Hi 142 [string] => hello 143 [2] => Array 144 ( 145 [] => World 146 [-2.34] => a 147 [0] => b 148 ) 149 150) 151 152 153--- Iteration 1 --- 154-- Inner iteration 0 of Iteration 1 -- 155Array 156( 157 [0] => string 158) 159 160-- Inner iteration 1 of Iteration 1 -- 161Array 162( 163 [0] => string 164 [1] => string 165) 166 167-- Inner iteration 2 of Iteration 1 -- 168Array 169( 170 [0] => string 171 [] => string 172) 173 174-- Inner iteration 3 of Iteration 1 -- 175Array 176( 177 [0] => string 178 [1] => 12 179) 180 181-- Inner iteration 4 of Iteration 1 -- 182Array 183( 184 [0] => string 185 [a] => 1 186 [b] => string 187 [c] => 188 [d] => -2.344 189) 190 191-- Inner iteration 5 of Iteration 1 -- 192Array 193( 194 [0] => string 195 [1] => 1 196 [2] => string 197 [3] => 198 [4] => -2.344 199) 200 201-- Inner iteration 6 of Iteration 1 -- 202Array 203( 204 [0] => string 205 [1] => 206 [2] => Hi 207 [string] => hello 208 [3] => Array 209 ( 210 [] => World 211 [-2.34] => a 212 [0] => b 213 ) 214 215) 216 217 218--- Iteration 2 --- 219-- Inner iteration 0 of Iteration 2 -- 220Array 221( 222 [] => string 223) 224 225-- Inner iteration 1 of Iteration 2 -- 226Array 227( 228 [] => string 229 [0] => string 230) 231 232-- Inner iteration 2 of Iteration 2 -- 233Array 234( 235 [] => string 236) 237 238-- Inner iteration 3 of Iteration 2 -- 239Array 240( 241 [] => string 242 [0] => 12 243) 244 245-- Inner iteration 4 of Iteration 2 -- 246Array 247( 248 [] => string 249 [a] => 1 250 [b] => string 251 [c] => 252 [d] => -2.344 253) 254 255-- Inner iteration 5 of Iteration 2 -- 256Array 257( 258 [] => string 259 [0] => 1 260 [1] => string 261 [2] => 262 [3] => -2.344 263) 264 265-- Inner iteration 6 of Iteration 2 -- 266Array 267( 268 [] => string 269 [0] => 270 [1] => Hi 271 [string] => hello 272 [2] => Array 273 ( 274 [] => World 275 [-2.34] => a 276 [0] => b 277 ) 278 279) 280 281 282--- Iteration 3 --- 283-- Inner iteration 0 of Iteration 3 -- 284Array 285( 286 [0] => 12 287) 288 289-- Inner iteration 1 of Iteration 3 -- 290Array 291( 292 [0] => 12 293 [1] => string 294) 295 296-- Inner iteration 2 of Iteration 3 -- 297Array 298( 299 [0] => 12 300 [] => string 301) 302 303-- Inner iteration 3 of Iteration 3 -- 304Array 305( 306 [0] => 12 307 [1] => 12 308) 309 310-- Inner iteration 4 of Iteration 3 -- 311Array 312( 313 [0] => 12 314 [a] => 1 315 [b] => string 316 [c] => 317 [d] => -2.344 318) 319 320-- Inner iteration 5 of Iteration 3 -- 321Array 322( 323 [0] => 12 324 [1] => 1 325 [2] => string 326 [3] => 327 [4] => -2.344 328) 329 330-- Inner iteration 6 of Iteration 3 -- 331Array 332( 333 [0] => 12 334 [1] => 335 [2] => Hi 336 [string] => hello 337 [3] => Array 338 ( 339 [] => World 340 [-2.34] => a 341 [0] => b 342 ) 343 344) 345 346 347--- Iteration 4 --- 348-- Inner iteration 0 of Iteration 4 -- 349Array 350( 351 [a] => 1 352 [b] => string 353 [c] => 354 [d] => -2.344 355) 356 357-- Inner iteration 1 of Iteration 4 -- 358Array 359( 360 [a] => 1 361 [b] => string 362 [c] => 363 [d] => -2.344 364 [0] => string 365) 366 367-- Inner iteration 2 of Iteration 4 -- 368Array 369( 370 [a] => 1 371 [b] => string 372 [c] => 373 [d] => -2.344 374 [] => string 375) 376 377-- Inner iteration 3 of Iteration 4 -- 378Array 379( 380 [a] => 1 381 [b] => string 382 [c] => 383 [d] => -2.344 384 [0] => 12 385) 386 387-- Inner iteration 4 of Iteration 4 -- 388Array 389( 390 [a] => 1 391 [b] => string 392 [c] => 393 [d] => -2.344 394) 395 396-- Inner iteration 5 of Iteration 4 -- 397Array 398( 399 [a] => 1 400 [b] => string 401 [c] => 402 [d] => -2.344 403 [0] => 1 404 [1] => string 405 [2] => 406 [3] => -2.344 407) 408 409-- Inner iteration 6 of Iteration 4 -- 410Array 411( 412 [a] => 1 413 [b] => string 414 [c] => 415 [d] => -2.344 416 [0] => 417 [1] => Hi 418 [string] => hello 419 [2] => Array 420 ( 421 [] => World 422 [-2.34] => a 423 [0] => b 424 ) 425 426) 427 428 429--- Iteration 5 --- 430-- Inner iteration 0 of Iteration 5 -- 431Array 432( 433 [0] => 1 434 [1] => string 435 [2] => 436 [3] => -2.344 437) 438 439-- Inner iteration 1 of Iteration 5 -- 440Array 441( 442 [0] => 1 443 [1] => string 444 [2] => 445 [3] => -2.344 446 [4] => string 447) 448 449-- Inner iteration 2 of Iteration 5 -- 450Array 451( 452 [0] => 1 453 [1] => string 454 [2] => 455 [3] => -2.344 456 [] => string 457) 458 459-- Inner iteration 3 of Iteration 5 -- 460Array 461( 462 [0] => 1 463 [1] => string 464 [2] => 465 [3] => -2.344 466 [4] => 12 467) 468 469-- Inner iteration 4 of Iteration 5 -- 470Array 471( 472 [0] => 1 473 [1] => string 474 [2] => 475 [3] => -2.344 476 [a] => 1 477 [b] => string 478 [c] => 479 [d] => -2.344 480) 481 482-- Inner iteration 5 of Iteration 5 -- 483Array 484( 485 [0] => 1 486 [1] => string 487 [2] => 488 [3] => -2.344 489 [4] => 1 490 [5] => string 491 [6] => 492 [7] => -2.344 493) 494 495-- Inner iteration 6 of Iteration 5 -- 496Array 497( 498 [0] => 1 499 [1] => string 500 [2] => 501 [3] => -2.344 502 [4] => 503 [5] => Hi 504 [string] => hello 505 [6] => Array 506 ( 507 [] => World 508 [-2.34] => a 509 [0] => b 510 ) 511 512) 513 514 515--- Iteration 6 --- 516-- Inner iteration 0 of Iteration 6 -- 517Array 518( 519 [0] => 520 [1] => Hi 521 [string] => hello 522 [2] => Array 523 ( 524 [] => World 525 [-2.34] => a 526 [0] => b 527 ) 528 529) 530 531-- Inner iteration 1 of Iteration 6 -- 532Array 533( 534 [0] => 535 [1] => Hi 536 [string] => hello 537 [2] => Array 538 ( 539 [] => World 540 [-2.34] => a 541 [0] => b 542 ) 543 544 [3] => string 545) 546 547-- Inner iteration 2 of Iteration 6 -- 548Array 549( 550 [0] => 551 [1] => Hi 552 [string] => hello 553 [2] => Array 554 ( 555 [] => World 556 [-2.34] => a 557 [0] => b 558 ) 559 560 [] => string 561) 562 563-- Inner iteration 3 of Iteration 6 -- 564Array 565( 566 [0] => 567 [1] => Hi 568 [string] => hello 569 [2] => Array 570 ( 571 [] => World 572 [-2.34] => a 573 [0] => b 574 ) 575 576 [3] => 12 577) 578 579-- Inner iteration 4 of Iteration 6 -- 580Array 581( 582 [0] => 583 [1] => Hi 584 [string] => hello 585 [2] => Array 586 ( 587 [] => World 588 [-2.34] => a 589 [0] => b 590 ) 591 592 [a] => 1 593 [b] => string 594 [c] => 595 [d] => -2.344 596) 597 598-- Inner iteration 5 of Iteration 6 -- 599Array 600( 601 [0] => 602 [1] => Hi 603 [string] => hello 604 [2] => Array 605 ( 606 [] => World 607 [-2.34] => a 608 [0] => b 609 ) 610 611 [3] => 1 612 [4] => string 613 [5] => 614 [6] => -2.344 615) 616 617-- Inner iteration 6 of Iteration 6 -- 618Array 619( 620 [0] => 621 [1] => Hi 622 [string] => hello 623 [2] => Array 624 ( 625 [] => World 626 [-2.34] => a 627 [0] => b 628 ) 629 630 [3] => 631 [4] => Hi 632 [5] => Array 633 ( 634 [] => World 635 [-2.34] => a 636 [0] => b 637 ) 638 639) 640 641*** Testing array_merge() with three or more arrays *** 642array(12) { 643 [0]=> 644 int(1) 645 [1]=> 646 string(6) "string" 647 [2]=> 648 NULL 649 [3]=> 650 float(-2.344) 651 ["a"]=> 652 int(1) 653 ["b"]=> 654 string(6) "string" 655 ["c"]=> 656 NULL 657 ["d"]=> 658 float(-2.344) 659 [4]=> 660 NULL 661 [5]=> 662 string(2) "Hi" 663 ["string"]=> 664 string(5) "hello" 665 [6]=> 666 array(3) { 667 [""]=> 668 string(5) "World" 669 ["-2.34"]=> 670 string(1) "a" 671 [0]=> 672 string(1) "b" 673 } 674} 675array(6) { 676 [0]=> 677 int(1) 678 [1]=> 679 string(6) "string" 680 [2]=> 681 NULL 682 [3]=> 683 float(-2.344) 684 [4]=> 685 string(12) "array on fly" 686 ["nullarray"]=> 687 array(0) { 688 } 689} 690 691*** Testing single array argument *** 692array(0) { 693} 694array(4) { 695 ["a"]=> 696 int(1) 697 ["b"]=> 698 string(6) "string" 699 ["c"]=> 700 NULL 701 ["d"]=> 702 float(-2.344) 703} 704array(4) { 705 [0]=> 706 int(1) 707 [1]=> 708 string(6) "string" 709 [2]=> 710 NULL 711 [3]=> 712 float(-2.344) 713} 714array(4) { 715 [0]=> 716 NULL 717 [1]=> 718 string(2) "Hi" 719 ["string"]=> 720 string(5) "hello" 721 [2]=> 722 array(3) { 723 [""]=> 724 string(5) "World" 725 ["-2.34"]=> 726 string(1) "a" 727 [0]=> 728 string(1) "b" 729 } 730} 731 732*** Testing array_merge() with typecasting non-array to array *** 733array(7) { 734 ["a"]=> 735 int(1) 736 ["b"]=> 737 string(6) "string" 738 ["c"]=> 739 NULL 740 ["d"]=> 741 float(-2.344) 742 [0]=> 743 string(5) "type1" 744 [1]=> 745 int(10) 746 [2]=> 747 float(12.34) 748} 749 750*** Testing error conditions *** 751Warning: array_merge(): Expected parameter 1 to be an array, int given in %s on line %d 752NULL 753 754Warning: array_merge(): Expected parameter 3 to be an array, int given in %s on line %d 755NULL 756 757Notice: Undefined variable: arr4 in %s on line %d 758 759Warning: array_merge(): Expected parameter 3 to be an array, null given in %s on line %d 760NULL 761 762*** Testing array_merge without any arguments *** 763array(0) { 764} 765Done 766