summaryrefslogtreecommitdiff
path: root/ext/standard/tests/array/array_column.phpt
blob: 030435a20cc18debfca35c68b00d06abf92dad55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
--TEST--
Test array_column_values() function
--FILE--
<?php
/* Prototype:
 *  array array_column(array $input, mixed $key);
 * Description:
 *  Returns an array containing all the values from
 *  the specified "column" in a two-dimensional array.
 */

echo "*** Testing basic functionalities ***\n";
/* Array representing a possible record set returned from a database */
$records = array(
	array(
		'id' => 1,
		'first_name' => 'John',
		'last_name' => 'Doe'
	),
	array(
		'id' => 2,
		'first_name' => 'Sally',
		'last_name' => 'Smith'
	),
	array(
		'id' => 3,
		'first_name' => 'Jane',
		'last_name' => 'Jones'
	)
);

echo "-- first_name column from recordset --\n";
var_dump(array_column($records, 'first_name'));

echo "-- id column from recordset --\n";
var_dump(array_column($records, 'id'));

echo "\n*** Testing multiple data types ***\n";
$file = basename(__FILE__);
$fh = fopen($file, 'r', true);
$values = array(
	array(
		'id' => 1,
		'value' => new stdClass
	),
	array(
		'id' => 2,
		'value' => 34.2345
	),
	array(
		'id' => 3,
		'value' => true
	),
	array(
		'id' => 4,
		'value' => false
	),
	array(
		'id' => 5,
		'value' => null
	),
	array(
		'id' => 6,
		'value' => 1234
	),
	array(
		'id' => 7,
		'value' => 'Foo'
	),
	array(
		'id' => 8,
		'value' => $fh
	)
);
var_dump(array_column($values, 'value'));

echo "\n*** Testing numeric column keys ***\n";
$numericCols = array(
	array('aaa', '111'),
	array('bbb', '222'),
	array('ccc', '333')
);
var_dump(array_column($numericCols, 1));

echo "\n*** Testing failure to find specified column ***\n";
var_dump(array_column($numericCols, 2));
var_dump(array_column($numericCols, 'foo'));

echo "\n*** Testing single dimensional array ***\n";
$singleDimension = array('foo', 'bar', 'baz');
var_dump(array_column($singleDimension, 1));

echo "\n*** Testing columns not present in all rows ***\n";
$mismatchedColumns = array(
    array('a' => 'foo', 'b' => 'bar'),
    array('a' => 'baz', 'c' => 'qux'),
);
var_dump(array_column($mismatchedColumns, 'c'));

echo "Done\n";
?>
--EXPECTF--
*** Testing basic functionalities ***
-- first_name column from recordset --
array(3) {
  [0]=>
  string(4) "John"
  [1]=>
  string(5) "Sally"
  [2]=>
  string(4) "Jane"
}
-- id column from recordset --
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

*** Testing multiple data types ***
array(8) {
  [0]=>
  object(stdClass)#1 (0) {
  }
  [1]=>
  float(34.2345)
  [2]=>
  bool(true)
  [3]=>
  bool(false)
  [4]=>
  NULL
  [5]=>
  int(1234)
  [6]=>
  string(3) "Foo"
  [7]=>
  resource(5) of type (stream)
}

*** Testing numeric column keys ***
array(3) {
  [0]=>
  string(3) "111"
  [1]=>
  string(3) "222"
  [2]=>
  string(3) "333"
}

*** Testing failure to find specified column ***
array(0) {
}
array(0) {
}

*** Testing single dimensional array ***
array(0) {
}

*** Testing columns not present in all rows ***
array(1) {
  [0]=>
  string(3) "qux"
}
Done