1--TEST-- 2Test array_column() function: testing with objects 3--FILE-- 4<?php 5 6class User 7{ 8 public $id, $first_name, $last_name; 9 10 public function __construct($id, $first_name, $last_name) 11 { 12 $this->id = $id; 13 $this->first_name = $first_name; 14 $this->last_name = $last_name; 15 } 16} 17 18function newUser($id, $first_name, $last_name) 19{ 20 $o = new stdClass; 21 $o->{0} = $id; 22 $o->{1} = $first_name; 23 $o->{2} = $last_name; 24 25 return $o; 26} 27 28class Something 29{ 30 public function __isset($name) 31 { 32 return $name == 'first_name'; 33 } 34 35 public function __get($name) 36 { 37 return new User(4, 'Jack', 'Sparrow'); 38 } 39} 40 41$records = array( 42 newUser(1, 'John', 'Doe'), 43 newUser(2, 'Sally', 'Smith'), 44 newUser(3, 'Jane', 'Jones'), 45 new User(1, 'John', 'Doe'), 46 new User(2, 'Sally', 'Smith'), 47 new User(3, 'Jane', 'Jones'), 48 new Something, 49); 50 51echo "*** Testing array_column() : object property fetching (numeric property names) ***\n"; 52 53echo "-- first_name column from recordset --\n"; 54var_dump(array_column($records, 1)); 55 56echo "-- id column from recordset --\n"; 57var_dump(array_column($records, 0)); 58 59echo "-- last_name column from recordset, keyed by value from id column --\n"; 60var_dump(array_column($records, 2, 0)); 61 62echo "-- last_name column from recordset, keyed by value from first_name column --\n"; 63var_dump(array_column($records, 2, 1)); 64 65echo "*** Testing array_column() : object property fetching (string property names) ***\n"; 66 67echo "-- first_name column from recordset --\n"; 68var_dump(array_column($records, 'first_name')); 69 70echo "-- id column from recordset --\n"; 71var_dump(array_column($records, 'id')); 72 73echo "-- last_name column from recordset, keyed by value from id column --\n"; 74var_dump(array_column($records, 'last_name', 'id')); 75 76echo "-- last_name column from recordset, keyed by value from first_name column --\n"; 77var_dump(array_column($records, 'last_name', 'first_name')); 78 79echo "Done\n"; 80?> 81--EXPECTF-- 82*** Testing array_column() : object property fetching (numeric property names) *** 83-- first_name column from recordset -- 84array(3) { 85 [0]=> 86 string(4) "John" 87 [1]=> 88 string(5) "Sally" 89 [2]=> 90 string(4) "Jane" 91} 92-- id column from recordset -- 93array(3) { 94 [0]=> 95 int(1) 96 [1]=> 97 int(2) 98 [2]=> 99 int(3) 100} 101-- last_name column from recordset, keyed by value from id column -- 102array(3) { 103 [1]=> 104 string(3) "Doe" 105 [2]=> 106 string(5) "Smith" 107 [3]=> 108 string(5) "Jones" 109} 110-- last_name column from recordset, keyed by value from first_name column -- 111array(3) { 112 ["John"]=> 113 string(3) "Doe" 114 ["Sally"]=> 115 string(5) "Smith" 116 ["Jane"]=> 117 string(5) "Jones" 118} 119*** Testing array_column() : object property fetching (string property names) *** 120-- first_name column from recordset -- 121array(4) { 122 [0]=> 123 string(4) "John" 124 [1]=> 125 string(5) "Sally" 126 [2]=> 127 string(4) "Jane" 128 [3]=> 129 object(User)#8 (3) { 130 ["id"]=> 131 int(4) 132 ["first_name"]=> 133 string(4) "Jack" 134 ["last_name"]=> 135 string(7) "Sparrow" 136 } 137} 138-- id column from recordset -- 139array(3) { 140 [0]=> 141 int(1) 142 [1]=> 143 int(2) 144 [2]=> 145 int(3) 146} 147-- last_name column from recordset, keyed by value from id column -- 148array(3) { 149 [1]=> 150 string(3) "Doe" 151 [2]=> 152 string(5) "Smith" 153 [3]=> 154 string(5) "Jones" 155} 156-- last_name column from recordset, keyed by value from first_name column -- 157array(3) { 158 ["John"]=> 159 string(3) "Doe" 160 ["Sally"]=> 161 string(5) "Smith" 162 ["Jane"]=> 163 string(5) "Jones" 164} 165Done 166