1--TEST--
2Test array_column() function: basic functionality
3--FILE--
4<?php
5
6echo "*** Testing array_column() : basic functionality ***\n";
7/* Array representing a possible record set returned from a database */
8$records = array(
9    array(
10        'id' => 1,
11        'first_name' => 'John',
12        'last_name' => 'Doe'
13    ),
14    array(
15        'id' => 2,
16        'first_name' => 'Sally',
17        'last_name' => 'Smith'
18    ),
19    array(
20        'id' => 3,
21        'first_name' => 'Jane',
22        'last_name' => 'Jones'
23    )
24);
25
26echo "-- first_name column from recordset --\n";
27var_dump(array_column($records, 'first_name'));
28
29echo "-- id column from recordset --\n";
30var_dump(array_column($records, 'id'));
31
32echo "-- last_name column from recordset, keyed by value from id column --\n";
33var_dump(array_column($records, 'last_name', 'id'));
34
35echo "-- last_name column from recordset, keyed by value from first_name column --\n";
36var_dump(array_column($records, 'last_name', 'first_name'));
37
38echo "\n*** Testing multiple data types ***\n";
39$fh = fopen(__FILE__, 'r', true);
40$values = array(
41    array(
42        'id' => 1,
43        'value' => new stdClass
44    ),
45    array(
46        'id' => 2,
47        'value' => 34.2345
48    ),
49    array(
50        'id' => 3,
51        'value' => true
52    ),
53    array(
54        'id' => 4,
55        'value' => false
56    ),
57    array(
58        'id' => 5,
59        'value' => null
60    ),
61    array(
62        'id' => 6,
63        'value' => 1234
64    ),
65    array(
66        'id' => 7,
67        'value' => 'Foo'
68    ),
69    array(
70        'id' => 8,
71        'value' => $fh
72    )
73);
74var_dump(array_column($values, 'value'));
75var_dump(array_column($values, 'value', 'id'));
76
77echo "\n*** Testing numeric column keys ***\n";
78$numericCols = array(
79    array('aaa', '111'),
80    array('bbb', '222'),
81    array('ccc', '333', -1 => 'ddd')
82);
83var_dump(array_column($numericCols, 1));
84var_dump(array_column($numericCols, 1, 0));
85var_dump(array_column($numericCols, 1, 0.123));
86var_dump(array_column($numericCols, 1, -1));
87
88echo "\n*** Testing failure to find specified column ***\n";
89var_dump(array_column($numericCols, 2));
90var_dump(array_column($numericCols, 'foo'));
91var_dump(array_column($numericCols, 0, 'foo'));
92var_dump(array_column($numericCols, 3.14));
93
94echo "\n*** Testing single dimensional array ***\n";
95$singleDimension = array('foo', 'bar', 'baz');
96var_dump(array_column($singleDimension, 1));
97
98echo "\n*** Testing columns not present in all rows ***\n";
99$mismatchedColumns = array(
100    array('a' => 'foo', 'b' => 'bar', 'e' => 'bbb'),
101    array('a' => 'baz', 'c' => 'qux', 'd' => 'aaa'),
102    array('a' => 'eee', 'b' => 'fff', 'e' => 'ggg'),
103);
104var_dump(array_column($mismatchedColumns, 'c'));
105var_dump(array_column($mismatchedColumns, 'c', 'a'));
106var_dump(array_column($mismatchedColumns, 'a', 'd'));
107var_dump(array_column($mismatchedColumns, 'a', 'e'));
108var_dump(array_column($mismatchedColumns, 'b'));
109var_dump(array_column($mismatchedColumns, 'b', 'a'));
110
111echo "\n*** Testing use of object converted to string ***\n";
112class Foo
113{
114    public function __toString()
115    {
116        return 'last_name';
117    }
118}
119class Bar
120{
121    public function __toString()
122    {
123        return 'first_name';
124    }
125}
126$f = new Foo();
127$b = new Bar();
128var_dump(array_column($records, $f));
129var_dump(array_column($records, $f, $b));
130
131echo "Done\n";
132?>
133--EXPECTF--
134*** Testing array_column() : basic functionality ***
135-- first_name column from recordset --
136array(3) {
137  [0]=>
138  string(4) "John"
139  [1]=>
140  string(5) "Sally"
141  [2]=>
142  string(4) "Jane"
143}
144-- id column from recordset --
145array(3) {
146  [0]=>
147  int(1)
148  [1]=>
149  int(2)
150  [2]=>
151  int(3)
152}
153-- last_name column from recordset, keyed by value from id column --
154array(3) {
155  [1]=>
156  string(3) "Doe"
157  [2]=>
158  string(5) "Smith"
159  [3]=>
160  string(5) "Jones"
161}
162-- last_name column from recordset, keyed by value from first_name column --
163array(3) {
164  ["John"]=>
165  string(3) "Doe"
166  ["Sally"]=>
167  string(5) "Smith"
168  ["Jane"]=>
169  string(5) "Jones"
170}
171
172*** Testing multiple data types ***
173array(8) {
174  [0]=>
175  object(stdClass)#%d (0) {
176  }
177  [1]=>
178  float(34.2345)
179  [2]=>
180  bool(true)
181  [3]=>
182  bool(false)
183  [4]=>
184  NULL
185  [5]=>
186  int(1234)
187  [6]=>
188  string(3) "Foo"
189  [7]=>
190  resource(%d) of type (stream)
191}
192array(8) {
193  [1]=>
194  object(stdClass)#%d (0) {
195  }
196  [2]=>
197  float(34.2345)
198  [3]=>
199  bool(true)
200  [4]=>
201  bool(false)
202  [5]=>
203  NULL
204  [6]=>
205  int(1234)
206  [7]=>
207  string(3) "Foo"
208  [8]=>
209  resource(%d) of type (stream)
210}
211
212*** Testing numeric column keys ***
213array(3) {
214  [0]=>
215  string(3) "111"
216  [1]=>
217  string(3) "222"
218  [2]=>
219  string(3) "333"
220}
221array(3) {
222  ["aaa"]=>
223  string(3) "111"
224  ["bbb"]=>
225  string(3) "222"
226  ["ccc"]=>
227  string(3) "333"
228}
229
230Deprecated: Implicit conversion from float 0.123 to int loses precision in %s on line %d
231array(3) {
232  ["aaa"]=>
233  string(3) "111"
234  ["bbb"]=>
235  string(3) "222"
236  ["ccc"]=>
237  string(3) "333"
238}
239array(3) {
240  [0]=>
241  string(3) "111"
242  [1]=>
243  string(3) "222"
244  ["ddd"]=>
245  string(3) "333"
246}
247
248*** Testing failure to find specified column ***
249array(0) {
250}
251array(0) {
252}
253array(3) {
254  [0]=>
255  string(3) "aaa"
256  [1]=>
257  string(3) "bbb"
258  [2]=>
259  string(3) "ccc"
260}
261
262Deprecated: Implicit conversion from float 3.14 to int loses precision in %s on line %d
263array(0) {
264}
265
266*** Testing single dimensional array ***
267array(0) {
268}
269
270*** Testing columns not present in all rows ***
271array(1) {
272  [0]=>
273  string(3) "qux"
274}
275array(1) {
276  ["baz"]=>
277  string(3) "qux"
278}
279array(3) {
280  [0]=>
281  string(3) "foo"
282  ["aaa"]=>
283  string(3) "baz"
284  [1]=>
285  string(3) "eee"
286}
287array(3) {
288  ["bbb"]=>
289  string(3) "foo"
290  [0]=>
291  string(3) "baz"
292  ["ggg"]=>
293  string(3) "eee"
294}
295array(2) {
296  [0]=>
297  string(3) "bar"
298  [1]=>
299  string(3) "fff"
300}
301array(2) {
302  ["foo"]=>
303  string(3) "bar"
304  ["eee"]=>
305  string(3) "fff"
306}
307
308*** Testing use of object converted to string ***
309array(3) {
310  [0]=>
311  string(3) "Doe"
312  [1]=>
313  string(5) "Smith"
314  [2]=>
315  string(5) "Jones"
316}
317array(3) {
318  ["John"]=>
319  string(3) "Doe"
320  ["Sally"]=>
321  string(5) "Smith"
322  ["Jane"]=>
323  string(5) "Jones"
324}
325Done
326