1--TEST--
2Test array_chunk() function : basic functionality - 'preserve_keys' as true/false
3--FILE--
4<?php
5/* Prototype  : array array_chunk(array $array, int $size [, bool $preserve_keys])
6 * Description: Split array into chunks
7 *              Chunks an array into size  large chunks.
8 * Source code: ext/standard/array.c
9*/
10
11echo "*** Testing array_chunk() : basic functionality ***\n";
12$size = 2;
13
14$input_arrays = array(
15  // array with default keys - numeric values
16  array(1, 2, 3, 4, 5),
17
18  // array with default keys - string values
19  array('value1', "value2", "value3"),
20
21  // associative arrays - key as string
22  array('key1' => 1, "key2" => 2, "key3" => 3),
23
24  // associative arrays - key as numeric
25  array(1 => 'one', 2 => "two", 3 => "three"),
26
27  // array containing elements with/without keys
28  array(1 => 'one','two', 3 => 'three', 4, "five" => 5)
29);
30
31$count = 1;
32// loop through each element of the array for input
33foreach ($input_arrays as $input_array){
34  echo "\n-- Iteration $count --\n";
35  var_dump( array_chunk($input_array, $size, true) );
36  var_dump( array_chunk($input_array, $size, false) );
37  $count++;
38}
39
40echo "Done"
41?>
42--EXPECTF--
43*** Testing array_chunk() : basic functionality ***
44
45-- Iteration 1 --
46array(3) {
47  [0]=>
48  array(2) {
49    [0]=>
50    int(1)
51    [1]=>
52    int(2)
53  }
54  [1]=>
55  array(2) {
56    [2]=>
57    int(3)
58    [3]=>
59    int(4)
60  }
61  [2]=>
62  array(1) {
63    [4]=>
64    int(5)
65  }
66}
67array(3) {
68  [0]=>
69  array(2) {
70    [0]=>
71    int(1)
72    [1]=>
73    int(2)
74  }
75  [1]=>
76  array(2) {
77    [0]=>
78    int(3)
79    [1]=>
80    int(4)
81  }
82  [2]=>
83  array(1) {
84    [0]=>
85    int(5)
86  }
87}
88
89-- Iteration 2 --
90array(2) {
91  [0]=>
92  array(2) {
93    [0]=>
94    string(6) "value1"
95    [1]=>
96    string(6) "value2"
97  }
98  [1]=>
99  array(1) {
100    [2]=>
101    string(6) "value3"
102  }
103}
104array(2) {
105  [0]=>
106  array(2) {
107    [0]=>
108    string(6) "value1"
109    [1]=>
110    string(6) "value2"
111  }
112  [1]=>
113  array(1) {
114    [0]=>
115    string(6) "value3"
116  }
117}
118
119-- Iteration 3 --
120array(2) {
121  [0]=>
122  array(2) {
123    ["key1"]=>
124    int(1)
125    ["key2"]=>
126    int(2)
127  }
128  [1]=>
129  array(1) {
130    ["key3"]=>
131    int(3)
132  }
133}
134array(2) {
135  [0]=>
136  array(2) {
137    [0]=>
138    int(1)
139    [1]=>
140    int(2)
141  }
142  [1]=>
143  array(1) {
144    [0]=>
145    int(3)
146  }
147}
148
149-- Iteration 4 --
150array(2) {
151  [0]=>
152  array(2) {
153    [1]=>
154    string(3) "one"
155    [2]=>
156    string(3) "two"
157  }
158  [1]=>
159  array(1) {
160    [3]=>
161    string(5) "three"
162  }
163}
164array(2) {
165  [0]=>
166  array(2) {
167    [0]=>
168    string(3) "one"
169    [1]=>
170    string(3) "two"
171  }
172  [1]=>
173  array(1) {
174    [0]=>
175    string(5) "three"
176  }
177}
178
179-- Iteration 5 --
180array(3) {
181  [0]=>
182  array(2) {
183    [1]=>
184    string(3) "one"
185    [2]=>
186    string(3) "two"
187  }
188  [1]=>
189  array(2) {
190    [3]=>
191    string(5) "three"
192    [4]=>
193    int(4)
194  }
195  [2]=>
196  array(1) {
197    ["five"]=>
198    int(5)
199  }
200}
201array(3) {
202  [0]=>
203  array(2) {
204    [0]=>
205    string(3) "one"
206    [1]=>
207    string(3) "two"
208  }
209  [1]=>
210  array(2) {
211    [0]=>
212    string(5) "three"
213    [1]=>
214    int(4)
215  }
216  [2]=>
217  array(1) {
218    [0]=>
219    int(5)
220  }
221}
222Done
223