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