array_unshift_variation3.phpt 8.0 KB

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