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
|
--TEST--
Test array_column() function: testing with objects
--FILE--
<?php
class User
{
public $id, $first_name, $last_name;
public function __construct($id, $first_name, $last_name)
{
$this->id = $id;
$this->first_name = $first_name;
$this->last_name = $last_name;
}
}
function newUser($id, $first_name, $last_name)
{
$o = new stdClass;
$o->{0} = $id;
$o->{1} = $first_name;
$o->{2} = $last_name;
return $o;
}
class Something
{
public function __isset($name)
{
return $name == 'first_name';
}
public function __get($name)
{
return new User(4, 'Jack', 'Sparrow');
}
}
$records = array(
newUser(1, 'John', 'Doe'),
newUser(2, 'Sally', 'Smith'),
newUser(3, 'Jane', 'Jones'),
new User(1, 'John', 'Doe'),
new User(2, 'Sally', 'Smith'),
new User(3, 'Jane', 'Jones'),
new Something,
);
echo "*** Testing array_column() : object property fetching (numeric property names) ***\n";
echo "-- first_name column from recordset --\n";
var_dump(array_column($records, 1));
echo "-- id column from recordset --\n";
var_dump(array_column($records, 0));
echo "-- last_name column from recordset, keyed by value from id column --\n";
var_dump(array_column($records, 2, 0));
echo "-- last_name column from recordset, keyed by value from first_name column --\n";
var_dump(array_column($records, 2, 1));
echo "*** Testing array_column() : object property fetching (string property names) ***\n";
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 "-- last_name column from recordset, keyed by value from id column --\n";
var_dump(array_column($records, 'last_name', 'id'));
echo "-- last_name column from recordset, keyed by value from first_name column --\n";
var_dump(array_column($records, 'last_name', 'first_name'));
echo "Done\n";
?>
--EXPECT--
*** Testing array_column() : object property fetching (numeric property names) ***
-- 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)
}
-- last_name column from recordset, keyed by value from id column --
array(3) {
[1]=>
string(3) "Doe"
[2]=>
string(5) "Smith"
[3]=>
string(5) "Jones"
}
-- last_name column from recordset, keyed by value from first_name column --
array(3) {
["John"]=>
string(3) "Doe"
["Sally"]=>
string(5) "Smith"
["Jane"]=>
string(5) "Jones"
}
*** Testing array_column() : object property fetching (string property names) ***
-- first_name column from recordset --
array(4) {
[0]=>
string(4) "John"
[1]=>
string(5) "Sally"
[2]=>
string(4) "Jane"
[3]=>
object(User)#8 (3) {
["id"]=>
int(4)
["first_name"]=>
string(4) "Jack"
["last_name"]=>
string(7) "Sparrow"
}
}
-- id column from recordset --
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
-- last_name column from recordset, keyed by value from id column --
array(3) {
[1]=>
string(3) "Doe"
[2]=>
string(5) "Smith"
[3]=>
string(5) "Jones"
}
-- last_name column from recordset, keyed by value from first_name column --
array(3) {
["John"]=>
string(3) "Doe"
["Sally"]=>
string(5) "Smith"
["Jane"]=>
string(5) "Jones"
}
Done
|