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