array_reverse_variation5.phpt 6.5 KB

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