array_reverse_variation4.phpt 6.2 KB

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