array_unshift_variation5.phpt 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. --TEST--
  2. Test array_unshift() function : usage variations - assoc. array with diff values for 'array' argument
  3. --INI--
  4. precision=12
  5. --FILE--
  6. <?php
  7. /* Prototype : int array_unshift(array $array, mixed $var [, mixed ...])
  8. * Description: Pushes elements onto the beginning of the array
  9. * Source code: ext/standard/array.c
  10. */
  11. /*
  12. * Testing the functionality of array_unshift() by passing different
  13. * associative arrays having different possible values to $array argument.
  14. * The $var argument passed is a fixed value
  15. */
  16. echo "*** Testing array_unshift() : associative array with different values ***\n";
  17. // get an unset variable
  18. $unset_var = 10;
  19. unset ($unset_var);
  20. // get a resource variable
  21. $fp = fopen(__FILE__, "r");
  22. // get a class
  23. class classA
  24. {
  25. public function __toString() {
  26. return "Class A object";
  27. }
  28. }
  29. // get a heredoc string
  30. $heredoc = <<<EOT
  31. Hello world
  32. EOT;
  33. // initializing $var argument
  34. $var = 10;
  35. // different variations of associative arrays to be passed to $array argument
  36. $arrays = array (
  37. // empty array
  38. /*1*/ array(),
  39. // arrays with integer values
  40. array('0' => 0),
  41. array("1" => 1),
  42. array("one" => 1, 'two' => 2, "three" => 3, 4 => 4),
  43. // arrays with float values
  44. /*5*/ array("float" => 2.3333),
  45. array("f1" => 1.2, 'f2' => 3.33, 3 => 4.89999922839999, 'f4' => 33333333.333333),
  46. // arrays with string values
  47. /*7*/ array(111 => "\tHello", "red" => "col\tor", 2 => "\v\fworld", 3.3 => "pen\n"),
  48. array(111 => '\tHello', "red" => 'col\tor', 2 => '\v\fworld', 3.3 => 'pen\n'),
  49. array(1 => "hello", "heredoc" => $heredoc),
  50. // array with object, unset variable and resource variable
  51. array(11 => new classA(), "unset" => @$unset_var, "resource" => $fp),
  52. // array with mixed values
  53. /*11*/ array(1 => 'hello', 2 => new classA(), 222 => "fruit", 'resource' => $fp, "int" => 133, "float" => 444.432, "unset" => @$unset_var, "heredoc" => $heredoc)
  54. );
  55. // loop through the various elements of $arrays to test array_unshift()
  56. $iterator = 1;
  57. foreach($arrays as $array) {
  58. echo "-- Iteration $iterator --\n";
  59. /* with default argument */
  60. // returns element count in the resulting array after arguments are pushed to
  61. // beginning of the given array
  62. $temp_array = $array;
  63. var_dump( array_unshift($temp_array, $var) );
  64. // dump the resulting array
  65. var_dump($temp_array);
  66. /* with optional arguments */
  67. // returns element count in the resulting array after arguments are pushed to
  68. // beginning of the given array
  69. $temp_array = $array;
  70. var_dump( array_unshift($temp_array, $var, "hello", 'world') );
  71. // dump the resulting array
  72. var_dump($temp_array);
  73. $iterator++;
  74. }
  75. echo "Done";
  76. ?>
  77. --EXPECTF--
  78. *** Testing array_unshift() : associative array with different values ***
  79. -- Iteration 1 --
  80. int(1)
  81. array(1) {
  82. [0]=>
  83. int(10)
  84. }
  85. int(3)
  86. array(3) {
  87. [0]=>
  88. int(10)
  89. [1]=>
  90. string(5) "hello"
  91. [2]=>
  92. string(5) "world"
  93. }
  94. -- Iteration 2 --
  95. int(2)
  96. array(2) {
  97. [0]=>
  98. int(10)
  99. [1]=>
  100. int(0)
  101. }
  102. int(4)
  103. array(4) {
  104. [0]=>
  105. int(10)
  106. [1]=>
  107. string(5) "hello"
  108. [2]=>
  109. string(5) "world"
  110. [3]=>
  111. int(0)
  112. }
  113. -- Iteration 3 --
  114. int(2)
  115. array(2) {
  116. [0]=>
  117. int(10)
  118. [1]=>
  119. int(1)
  120. }
  121. int(4)
  122. array(4) {
  123. [0]=>
  124. int(10)
  125. [1]=>
  126. string(5) "hello"
  127. [2]=>
  128. string(5) "world"
  129. [3]=>
  130. int(1)
  131. }
  132. -- Iteration 4 --
  133. int(5)
  134. array(5) {
  135. [0]=>
  136. int(10)
  137. ["one"]=>
  138. int(1)
  139. ["two"]=>
  140. int(2)
  141. ["three"]=>
  142. int(3)
  143. [1]=>
  144. int(4)
  145. }
  146. int(7)
  147. array(7) {
  148. [0]=>
  149. int(10)
  150. [1]=>
  151. string(5) "hello"
  152. [2]=>
  153. string(5) "world"
  154. ["one"]=>
  155. int(1)
  156. ["two"]=>
  157. int(2)
  158. ["three"]=>
  159. int(3)
  160. [3]=>
  161. int(4)
  162. }
  163. -- Iteration 5 --
  164. int(2)
  165. array(2) {
  166. [0]=>
  167. int(10)
  168. ["float"]=>
  169. float(2.3333)
  170. }
  171. int(4)
  172. array(4) {
  173. [0]=>
  174. int(10)
  175. [1]=>
  176. string(5) "hello"
  177. [2]=>
  178. string(5) "world"
  179. ["float"]=>
  180. float(2.3333)
  181. }
  182. -- Iteration 6 --
  183. int(5)
  184. array(5) {
  185. [0]=>
  186. int(10)
  187. ["f1"]=>
  188. float(1.2)
  189. ["f2"]=>
  190. float(3.33)
  191. [1]=>
  192. float(4.8999992284)
  193. ["f4"]=>
  194. float(33333333.3333)
  195. }
  196. int(7)
  197. array(7) {
  198. [0]=>
  199. int(10)
  200. [1]=>
  201. string(5) "hello"
  202. [2]=>
  203. string(5) "world"
  204. ["f1"]=>
  205. float(1.2)
  206. ["f2"]=>
  207. float(3.33)
  208. [3]=>
  209. float(4.8999992284)
  210. ["f4"]=>
  211. float(33333333.3333)
  212. }
  213. -- Iteration 7 --
  214. int(5)
  215. array(5) {
  216. [0]=>
  217. int(10)
  218. [1]=>
  219. string(6) " Hello"
  220. ["red"]=>
  221. string(6) "col or"
  222. [2]=>
  223. string(7) " world"
  224. [3]=>
  225. string(4) "pen
  226. "
  227. }
  228. int(7)
  229. array(7) {
  230. [0]=>
  231. int(10)
  232. [1]=>
  233. string(5) "hello"
  234. [2]=>
  235. string(5) "world"
  236. [3]=>
  237. string(6) " Hello"
  238. ["red"]=>
  239. string(6) "col or"
  240. [4]=>
  241. string(7) " world"
  242. [5]=>
  243. string(4) "pen
  244. "
  245. }
  246. -- Iteration 8 --
  247. int(5)
  248. array(5) {
  249. [0]=>
  250. int(10)
  251. [1]=>
  252. string(7) "\tHello"
  253. ["red"]=>
  254. string(7) "col\tor"
  255. [2]=>
  256. string(9) "\v\fworld"
  257. [3]=>
  258. string(5) "pen\n"
  259. }
  260. int(7)
  261. array(7) {
  262. [0]=>
  263. int(10)
  264. [1]=>
  265. string(5) "hello"
  266. [2]=>
  267. string(5) "world"
  268. [3]=>
  269. string(7) "\tHello"
  270. ["red"]=>
  271. string(7) "col\tor"
  272. [4]=>
  273. string(9) "\v\fworld"
  274. [5]=>
  275. string(5) "pen\n"
  276. }
  277. -- Iteration 9 --
  278. int(3)
  279. array(3) {
  280. [0]=>
  281. int(10)
  282. [1]=>
  283. string(5) "hello"
  284. ["heredoc"]=>
  285. string(11) "Hello world"
  286. }
  287. int(5)
  288. array(5) {
  289. [0]=>
  290. int(10)
  291. [1]=>
  292. string(5) "hello"
  293. [2]=>
  294. string(5) "world"
  295. [3]=>
  296. string(5) "hello"
  297. ["heredoc"]=>
  298. string(11) "Hello world"
  299. }
  300. -- Iteration 10 --
  301. int(4)
  302. array(4) {
  303. [0]=>
  304. int(10)
  305. [1]=>
  306. object(classA)#%d (0) {
  307. }
  308. ["unset"]=>
  309. NULL
  310. ["resource"]=>
  311. resource(%d) of type (stream)
  312. }
  313. int(6)
  314. array(6) {
  315. [0]=>
  316. int(10)
  317. [1]=>
  318. string(5) "hello"
  319. [2]=>
  320. string(5) "world"
  321. [3]=>
  322. object(classA)#%d (0) {
  323. }
  324. ["unset"]=>
  325. NULL
  326. ["resource"]=>
  327. resource(%d) of type (stream)
  328. }
  329. -- Iteration 11 --
  330. int(9)
  331. array(9) {
  332. [0]=>
  333. int(10)
  334. [1]=>
  335. string(5) "hello"
  336. [2]=>
  337. object(classA)#%d (0) {
  338. }
  339. [3]=>
  340. string(5) "fruit"
  341. ["resource"]=>
  342. resource(%d) of type (stream)
  343. ["int"]=>
  344. int(133)
  345. ["float"]=>
  346. float(444.432)
  347. ["unset"]=>
  348. NULL
  349. ["heredoc"]=>
  350. string(11) "Hello world"
  351. }
  352. int(11)
  353. array(11) {
  354. [0]=>
  355. int(10)
  356. [1]=>
  357. string(5) "hello"
  358. [2]=>
  359. string(5) "world"
  360. [3]=>
  361. string(5) "hello"
  362. [4]=>
  363. object(classA)#%d (0) {
  364. }
  365. [5]=>
  366. string(5) "fruit"
  367. ["resource"]=>
  368. resource(%d) of type (stream)
  369. ["int"]=>
  370. int(133)
  371. ["float"]=>
  372. float(444.432)
  373. ["unset"]=>
  374. NULL
  375. ["heredoc"]=>
  376. string(11) "Hello world"
  377. }
  378. Done