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