array_merge_variation3.phpt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. --TEST--
  2. Test array_merge() function : usage variations - arrays of diff. data types
  3. --FILE--
  4. <?php
  5. /* Prototype : array array_merge(array $arr1, array $arr2 [, array $...])
  6. * Description: Merges elements from passed arrays into one array
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * Pass arrays of different data types to test how array_merge adds them
  11. * onto an existing array
  12. */
  13. echo "*** Testing array_merge() : usage variations ***\n";
  14. // Initialise function arguments not being substituted
  15. $arr = array (1, 2);
  16. //get an unset variable
  17. $unset_var = 10;
  18. unset ($unset_var);
  19. // get a class
  20. class classA
  21. {
  22. public function __toString() {
  23. return "Class A object";
  24. }
  25. }
  26. // heredoc string
  27. $heredoc = <<<EOT
  28. hello world
  29. EOT;
  30. // get a resource variable
  31. $fp = fopen(__FILE__, "r");
  32. // arrays of different data types to be passed as $input
  33. $inputs = array(
  34. // int data
  35. /*1*/ 'int' => array(
  36. 0,
  37. 1,
  38. 12345,
  39. -2345,
  40. ),
  41. // float data
  42. /*2*/ 'float' => array(
  43. 10.5,
  44. -10.5,
  45. 12.3456789000e10,
  46. 12.3456789000E-10,
  47. .5,
  48. ),
  49. // null data
  50. /*3*/ 'null' => array(
  51. NULL,
  52. null,
  53. ),
  54. // boolean data
  55. /*4*/ 'bool' => array(
  56. true,
  57. false,
  58. TRUE,
  59. FALSE,
  60. ),
  61. // empty data
  62. /*5*/ 'empty string' => array(
  63. "",
  64. '',
  65. ),
  66. /*6*/ 'empty array' => array(
  67. ),
  68. // string data
  69. /*7*/ 'string' => array(
  70. "string",
  71. 'string',
  72. $heredoc,
  73. ),
  74. // object data
  75. /*8*/ 'object' => array(
  76. new classA(),
  77. ),
  78. // undefined data
  79. /*9*/ 'undefined' => array(
  80. @$undefined_var,
  81. ),
  82. // unset data
  83. /*10*/ 'unset' => array(
  84. @$unset_var,
  85. ),
  86. // resource variable
  87. /*11*/ 'resource' => array(
  88. $fp
  89. ),
  90. );
  91. // loop through each element of $inputs to check the behavior of array_merge
  92. $iterator = 1;
  93. foreach($inputs as $key => $input) {
  94. echo "\n-- Iteration $iterator: $key data --\n";
  95. var_dump( array_merge($input, $arr) );
  96. var_dump( array_merge($arr, $input) );
  97. $iterator++;
  98. };
  99. fclose($fp);
  100. echo "Done";
  101. ?>
  102. --EXPECTF--
  103. *** Testing array_merge() : usage variations ***
  104. -- Iteration 1: int data --
  105. array(6) {
  106. [0]=>
  107. int(0)
  108. [1]=>
  109. int(1)
  110. [2]=>
  111. int(12345)
  112. [3]=>
  113. int(-2345)
  114. [4]=>
  115. int(1)
  116. [5]=>
  117. int(2)
  118. }
  119. array(6) {
  120. [0]=>
  121. int(1)
  122. [1]=>
  123. int(2)
  124. [2]=>
  125. int(0)
  126. [3]=>
  127. int(1)
  128. [4]=>
  129. int(12345)
  130. [5]=>
  131. int(-2345)
  132. }
  133. -- Iteration 2: float data --
  134. array(7) {
  135. [0]=>
  136. float(10.5)
  137. [1]=>
  138. float(-10.5)
  139. [2]=>
  140. float(123456789000)
  141. [3]=>
  142. float(1.23456789E-9)
  143. [4]=>
  144. float(0.5)
  145. [5]=>
  146. int(1)
  147. [6]=>
  148. int(2)
  149. }
  150. array(7) {
  151. [0]=>
  152. int(1)
  153. [1]=>
  154. int(2)
  155. [2]=>
  156. float(10.5)
  157. [3]=>
  158. float(-10.5)
  159. [4]=>
  160. float(123456789000)
  161. [5]=>
  162. float(1.23456789E-9)
  163. [6]=>
  164. float(0.5)
  165. }
  166. -- Iteration 3: null data --
  167. array(4) {
  168. [0]=>
  169. NULL
  170. [1]=>
  171. NULL
  172. [2]=>
  173. int(1)
  174. [3]=>
  175. int(2)
  176. }
  177. array(4) {
  178. [0]=>
  179. int(1)
  180. [1]=>
  181. int(2)
  182. [2]=>
  183. NULL
  184. [3]=>
  185. NULL
  186. }
  187. -- Iteration 4: bool data --
  188. array(6) {
  189. [0]=>
  190. bool(true)
  191. [1]=>
  192. bool(false)
  193. [2]=>
  194. bool(true)
  195. [3]=>
  196. bool(false)
  197. [4]=>
  198. int(1)
  199. [5]=>
  200. int(2)
  201. }
  202. array(6) {
  203. [0]=>
  204. int(1)
  205. [1]=>
  206. int(2)
  207. [2]=>
  208. bool(true)
  209. [3]=>
  210. bool(false)
  211. [4]=>
  212. bool(true)
  213. [5]=>
  214. bool(false)
  215. }
  216. -- Iteration 5: empty string data --
  217. array(4) {
  218. [0]=>
  219. string(0) ""
  220. [1]=>
  221. string(0) ""
  222. [2]=>
  223. int(1)
  224. [3]=>
  225. int(2)
  226. }
  227. array(4) {
  228. [0]=>
  229. int(1)
  230. [1]=>
  231. int(2)
  232. [2]=>
  233. string(0) ""
  234. [3]=>
  235. string(0) ""
  236. }
  237. -- Iteration 6: empty array data --
  238. array(2) {
  239. [0]=>
  240. int(1)
  241. [1]=>
  242. int(2)
  243. }
  244. array(2) {
  245. [0]=>
  246. int(1)
  247. [1]=>
  248. int(2)
  249. }
  250. -- Iteration 7: string data --
  251. array(5) {
  252. [0]=>
  253. string(6) "string"
  254. [1]=>
  255. string(6) "string"
  256. [2]=>
  257. string(11) "hello world"
  258. [3]=>
  259. int(1)
  260. [4]=>
  261. int(2)
  262. }
  263. array(5) {
  264. [0]=>
  265. int(1)
  266. [1]=>
  267. int(2)
  268. [2]=>
  269. string(6) "string"
  270. [3]=>
  271. string(6) "string"
  272. [4]=>
  273. string(11) "hello world"
  274. }
  275. -- Iteration 8: object data --
  276. array(3) {
  277. [0]=>
  278. object(classA)#%d (0) {
  279. }
  280. [1]=>
  281. int(1)
  282. [2]=>
  283. int(2)
  284. }
  285. array(3) {
  286. [0]=>
  287. int(1)
  288. [1]=>
  289. int(2)
  290. [2]=>
  291. object(classA)#%d (0) {
  292. }
  293. }
  294. -- Iteration 9: undefined data --
  295. array(3) {
  296. [0]=>
  297. NULL
  298. [1]=>
  299. int(1)
  300. [2]=>
  301. int(2)
  302. }
  303. array(3) {
  304. [0]=>
  305. int(1)
  306. [1]=>
  307. int(2)
  308. [2]=>
  309. NULL
  310. }
  311. -- Iteration 10: unset data --
  312. array(3) {
  313. [0]=>
  314. NULL
  315. [1]=>
  316. int(1)
  317. [2]=>
  318. int(2)
  319. }
  320. array(3) {
  321. [0]=>
  322. int(1)
  323. [1]=>
  324. int(2)
  325. [2]=>
  326. NULL
  327. }
  328. -- Iteration 11: resource data --
  329. array(3) {
  330. [0]=>
  331. resource(%d) of type (stream)
  332. [1]=>
  333. int(1)
  334. [2]=>
  335. int(2)
  336. }
  337. array(3) {
  338. [0]=>
  339. int(1)
  340. [1]=>
  341. int(2)
  342. [2]=>
  343. resource(%d) of type (stream)
  344. }
  345. Done