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} 229array(3) { 230 ["aaa"]=> 231 string(3) "111" 232 ["bbb"]=> 233 string(3) "222" 234 ["ccc"]=> 235 string(3) "333" 236} 237array(3) { 238 [0]=> 239 string(3) "111" 240 [1]=> 241 string(3) "222" 242 ["ddd"]=> 243 string(3) "333" 244} 245 246*** Testing failure to find specified column *** 247array(0) { 248} 249array(0) { 250} 251array(3) { 252 [0]=> 253 string(3) "aaa" 254 [1]=> 255 string(3) "bbb" 256 [2]=> 257 string(3) "ccc" 258} 259array(0) { 260} 261 262*** Testing single dimensional array *** 263array(0) { 264} 265 266*** Testing columns not present in all rows *** 267array(1) { 268 [0]=> 269 string(3) "qux" 270} 271array(1) { 272 ["baz"]=> 273 string(3) "qux" 274} 275array(3) { 276 [0]=> 277 string(3) "foo" 278 ["aaa"]=> 279 string(3) "baz" 280 [1]=> 281 string(3) "eee" 282} 283array(3) { 284 ["bbb"]=> 285 string(3) "foo" 286 [0]=> 287 string(3) "baz" 288 ["ggg"]=> 289 string(3) "eee" 290} 291array(2) { 292 [0]=> 293 string(3) "bar" 294 [1]=> 295 string(3) "fff" 296} 297array(2) { 298 ["foo"]=> 299 string(3) "bar" 300 ["eee"]=> 301 string(3) "fff" 302} 303 304*** Testing use of object converted to string *** 305array(3) { 306 [0]=> 307 string(3) "Doe" 308 [1]=> 309 string(5) "Smith" 310 [2]=> 311 string(5) "Jones" 312} 313array(3) { 314 ["John"]=> 315 string(3) "Doe" 316 ["Sally"]=> 317 string(5) "Smith" 318 ["Jane"]=> 319 string(5) "Jones" 320} 321Done 322