array_reverse_variation3.phpt 9.1 KB

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