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--EXPECTF--
114*** Testing array_slice() : usage variations ***
115
116-- Iteration 1 : key type is int --
117$preserve_keys = TRUE
118array(4) {
119  [0]=>
120  string(4) "zero"
121  [1]=>
122  string(3) "one"
123  [12345]=>
124  string(8) "positive"
125  [-2345]=>
126  string(8) "negative"
127}
128$preserve_keys = FALSE
129array(4) {
130  [0]=>
131  string(4) "zero"
132  [1]=>
133  string(3) "one"
134  [2]=>
135  string(8) "positive"
136  [3]=>
137  string(8) "negative"
138}
139
140-- Iteration 2 : key type is float --
141$preserve_keys = TRUE
142array(3) {
143  [10]=>
144  string(8) "positive"
145  [-10]=>
146  string(8) "negative"
147  [0]=>
148  string(4) "half"
149}
150$preserve_keys = FALSE
151array(3) {
152  [0]=>
153  string(8) "positive"
154  [1]=>
155  string(8) "negative"
156  [2]=>
157  string(4) "half"
158}
159
160-- Iteration 3 : key type is extreme floats --
161$preserve_keys = TRUE
162array(2) {
163  [12345678]=>
164  string(5) "large"
165  [0]=>
166  string(5) "small"
167}
168$preserve_keys = FALSE
169array(2) {
170  [0]=>
171  string(5) "large"
172  [1]=>
173  string(5) "small"
174}
175
176-- Iteration 4 : key type is null uppercase --
177$preserve_keys = TRUE
178array(1) {
179  [""]=>
180  string(6) "null 1"
181}
182$preserve_keys = FALSE
183array(1) {
184  [""]=>
185  string(6) "null 1"
186}
187
188-- Iteration 5 : key type is null lowercase --
189$preserve_keys = TRUE
190array(1) {
191  [""]=>
192  string(6) "null 2"
193}
194$preserve_keys = FALSE
195array(1) {
196  [""]=>
197  string(6) "null 2"
198}
199
200-- Iteration 6 : key type is bool lowercase --
201$preserve_keys = TRUE
202array(2) {
203  [1]=>
204  string(6) "lowert"
205  [0]=>
206  string(6) "lowerf"
207}
208$preserve_keys = FALSE
209array(2) {
210  [0]=>
211  string(6) "lowert"
212  [1]=>
213  string(6) "lowerf"
214}
215
216-- Iteration 7 : key type is bool uppercase --
217$preserve_keys = TRUE
218array(2) {
219  [1]=>
220  string(6) "uppert"
221  [0]=>
222  string(6) "upperf"
223}
224$preserve_keys = FALSE
225array(2) {
226  [0]=>
227  string(6) "uppert"
228  [1]=>
229  string(6) "upperf"
230}
231
232-- Iteration 8 : key type is empty double quotes --
233$preserve_keys = TRUE
234array(1) {
235  [""]=>
236  string(6) "emptyd"
237}
238$preserve_keys = FALSE
239array(1) {
240  [""]=>
241  string(6) "emptyd"
242}
243
244-- Iteration 9 : key type is empty single quotes --
245$preserve_keys = TRUE
246array(1) {
247  [""]=>
248  string(6) "emptys"
249}
250$preserve_keys = FALSE
251array(1) {
252  [""]=>
253  string(6) "emptys"
254}
255
256-- Iteration 10 : key type is string --
257$preserve_keys = TRUE
258array(3) {
259  ["stringd"]=>
260  string(7) "stringd"
261  ["strings"]=>
262  string(7) "strings"
263  ["hello world"]=>
264  string(7) "stringh"
265}
266$preserve_keys = FALSE
267array(3) {
268  ["stringd"]=>
269  string(7) "stringd"
270  ["strings"]=>
271  string(7) "strings"
272  ["hello world"]=>
273  string(7) "stringh"
274}
275
276-- Iteration 11 : key type is undefined --
277$preserve_keys = TRUE
278array(1) {
279  [""]=>
280  string(9) "undefined"
281}
282$preserve_keys = FALSE
283array(1) {
284  [""]=>
285  string(9) "undefined"
286}
287
288-- Iteration 12 : key type is unset --
289$preserve_keys = TRUE
290array(1) {
291  [""]=>
292  string(5) "unset"
293}
294$preserve_keys = FALSE
295array(1) {
296  [""]=>
297  string(5) "unset"
298}
299Done
300