array_slice_variation7.phpt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. --TEST--
  2. Test array_slice() function : usage variations - different data types as keys in an array
  3. --FILE--
  4. <?php
  5. /* Prototype : array array_slice(array $input, int $offset [, int $length [, bool $preserve_keys]])
  6. * Description: Returns elements specified by offset and length
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * Pass different data types as keys in an array to array_slice()
  11. * to test how $preserve_keys treats them
  12. */
  13. echo "*** Testing array_slice() : usage variations ***\n";
  14. // Initialise function arguments not being substituted
  15. $offset = 0;
  16. $length = 10; // to ensure all elements are displayed
  17. //get an unset variable
  18. $unset_var = 10;
  19. unset ($unset_var);
  20. // heredoc string
  21. $heredoc = <<<EOT
  22. hello world
  23. EOT;
  24. // arrays of different data types to be passed as $input
  25. $inputs = array(
  26. // int data
  27. /*1*/ 'int' => array(
  28. 0 => 'zero',
  29. 1 => 'one',
  30. 12345 => 'positive',
  31. -2345 => 'negative',
  32. ),
  33. // float data
  34. /*2*/ 'float' => array(
  35. 10.5 => 'positive',
  36. -10.5 => 'negative',
  37. .5 => 'half',
  38. ),
  39. /*3*/ 'extreme floats' => array(
  40. 12.3456789000e6 => 'large',
  41. 12.3456789000E-10 => 'small',
  42. ),
  43. // null data
  44. /*4*/ 'null uppercase' => array(
  45. NULL => 'null 1',
  46. ),
  47. /*5*/ 'null lowercase' => array(
  48. null => 'null 2',
  49. ),
  50. // boolean data
  51. /*6*/ 'bool lowercase' => array(
  52. true => 'lowert',
  53. false => 'lowerf',
  54. ),
  55. /*7*/ 'bool uppercase' => array(
  56. TRUE => 'uppert',
  57. FALSE => 'upperf',
  58. ),
  59. // empty data
  60. /*8*/ 'empty double quotes' => array(
  61. "" => 'emptyd',
  62. ),
  63. /*9*/ 'empty single quotes' => array(
  64. '' => 'emptys',
  65. ),
  66. // string data
  67. /*10*/ 'string' => array(
  68. "stringd" => 'stringd',
  69. 'strings' => 'strings',
  70. $heredoc => 'stringh',
  71. ),
  72. // undefined data
  73. /*11*/ 'undefined' => array(
  74. @$undefined_var => 'undefined',
  75. ),
  76. // unset data
  77. /*12*/ 'unset' => array(
  78. @$unset_var => 'unset',
  79. ),
  80. );
  81. // loop through each element of $inputs to check the behavior of array_slice()
  82. $iterator = 1;
  83. foreach($inputs as $type => $input) {
  84. echo "\n-- Iteration $iterator : key type is $type --\n";
  85. echo "\$preserve_keys = TRUE\n";
  86. var_dump( array_slice($input, $offset, $length, true) );
  87. echo "\$preserve_keys = FALSE\n";
  88. var_dump( array_slice($input, $offset, $length, false) );
  89. $iterator++;
  90. };
  91. echo "Done";
  92. ?>
  93. --EXPECTF--
  94. *** Testing array_slice() : usage variations ***
  95. -- Iteration 1 : key type is int --
  96. $preserve_keys = TRUE
  97. array(4) {
  98. [0]=>
  99. string(4) "zero"
  100. [1]=>
  101. string(3) "one"
  102. [12345]=>
  103. string(8) "positive"
  104. [-2345]=>
  105. string(8) "negative"
  106. }
  107. $preserve_keys = FALSE
  108. array(4) {
  109. [0]=>
  110. string(4) "zero"
  111. [1]=>
  112. string(3) "one"
  113. [2]=>
  114. string(8) "positive"
  115. [3]=>
  116. string(8) "negative"
  117. }
  118. -- Iteration 2 : key type is float --
  119. $preserve_keys = TRUE
  120. array(3) {
  121. [10]=>
  122. string(8) "positive"
  123. [-10]=>
  124. string(8) "negative"
  125. [0]=>
  126. string(4) "half"
  127. }
  128. $preserve_keys = FALSE
  129. array(3) {
  130. [0]=>
  131. string(8) "positive"
  132. [1]=>
  133. string(8) "negative"
  134. [2]=>
  135. string(4) "half"
  136. }
  137. -- Iteration 3 : key type is extreme floats --
  138. $preserve_keys = TRUE
  139. array(2) {
  140. [12345678]=>
  141. string(5) "large"
  142. [0]=>
  143. string(5) "small"
  144. }
  145. $preserve_keys = FALSE
  146. array(2) {
  147. [0]=>
  148. string(5) "large"
  149. [1]=>
  150. string(5) "small"
  151. }
  152. -- Iteration 4 : key type is null uppercase --
  153. $preserve_keys = TRUE
  154. array(1) {
  155. [""]=>
  156. string(6) "null 1"
  157. }
  158. $preserve_keys = FALSE
  159. array(1) {
  160. [""]=>
  161. string(6) "null 1"
  162. }
  163. -- Iteration 5 : key type is null lowercase --
  164. $preserve_keys = TRUE
  165. array(1) {
  166. [""]=>
  167. string(6) "null 2"
  168. }
  169. $preserve_keys = FALSE
  170. array(1) {
  171. [""]=>
  172. string(6) "null 2"
  173. }
  174. -- Iteration 6 : key type is bool lowercase --
  175. $preserve_keys = TRUE
  176. array(2) {
  177. [1]=>
  178. string(6) "lowert"
  179. [0]=>
  180. string(6) "lowerf"
  181. }
  182. $preserve_keys = FALSE
  183. array(2) {
  184. [0]=>
  185. string(6) "lowert"
  186. [1]=>
  187. string(6) "lowerf"
  188. }
  189. -- Iteration 7 : key type is bool uppercase --
  190. $preserve_keys = TRUE
  191. array(2) {
  192. [1]=>
  193. string(6) "uppert"
  194. [0]=>
  195. string(6) "upperf"
  196. }
  197. $preserve_keys = FALSE
  198. array(2) {
  199. [0]=>
  200. string(6) "uppert"
  201. [1]=>
  202. string(6) "upperf"
  203. }
  204. -- Iteration 8 : key type is empty double quotes --
  205. $preserve_keys = TRUE
  206. array(1) {
  207. [""]=>
  208. string(6) "emptyd"
  209. }
  210. $preserve_keys = FALSE
  211. array(1) {
  212. [""]=>
  213. string(6) "emptyd"
  214. }
  215. -- Iteration 9 : key type is empty single quotes --
  216. $preserve_keys = TRUE
  217. array(1) {
  218. [""]=>
  219. string(6) "emptys"
  220. }
  221. $preserve_keys = FALSE
  222. array(1) {
  223. [""]=>
  224. string(6) "emptys"
  225. }
  226. -- Iteration 10 : key type is string --
  227. $preserve_keys = TRUE
  228. array(3) {
  229. ["stringd"]=>
  230. string(7) "stringd"
  231. ["strings"]=>
  232. string(7) "strings"
  233. ["hello world"]=>
  234. string(7) "stringh"
  235. }
  236. $preserve_keys = FALSE
  237. array(3) {
  238. ["stringd"]=>
  239. string(7) "stringd"
  240. ["strings"]=>
  241. string(7) "strings"
  242. ["hello world"]=>
  243. string(7) "stringh"
  244. }
  245. -- Iteration 11 : key type is undefined --
  246. $preserve_keys = TRUE
  247. array(1) {
  248. [""]=>
  249. string(9) "undefined"
  250. }
  251. $preserve_keys = FALSE
  252. array(1) {
  253. [""]=>
  254. string(9) "undefined"
  255. }
  256. -- Iteration 12 : key type is unset --
  257. $preserve_keys = TRUE
  258. array(1) {
  259. [""]=>
  260. string(5) "unset"
  261. }
  262. $preserve_keys = FALSE
  263. array(1) {
  264. [""]=>
  265. string(5) "unset"
  266. }
  267. Done