array_merge.phpt 12 KB

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