-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy patharray-column.xml
318 lines (300 loc) · 7.13 KB
/
array-column.xml
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 8bf3587d8f70239a65d9aa44d42ced8a696a3e86 Maintainer: 邹松 Status: ready -->
<!-- CREDITS: mowangjuanzi -->
<refentry xml:id="function.array-column" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>array_column</refname>
<refpurpose>返回输入数组中指定列的值</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>array</type><methodname>array_column</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam><type class="union"><type>int</type><type>string</type><type>null</type></type><parameter>column_key</parameter></methodparam>
<methodparam choice="opt"><type class="union"><type>int</type><type>string</type><type>null</type></type><parameter>index_key</parameter><initializer>&null;</initializer></methodparam>
</methodsynopsis>
<para>
<function>array_column</function> 返回
<parameter>array</parameter> 中键名为
<parameter>column_key</parameter> 的一列值。 如果指定了可选参数
<parameter>index_key</parameter>,则使用输入数组中
<parameter>index_key</parameter> 列的值将作为返回数组中对应值的键。
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>array</parameter></term>
<listitem>
<para>
多维数组或对象数组,从中提取一列值。
如果提供的是对象数组,只有 public 的属性会被直接取出。
如果想取出 private 和 protected 的属性,类必须实现
<function>__get</function> 和 <function>__isset</function> 魔术方法。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>column_key</parameter></term>
<listitem>
<para>
需要返回值的列。它可以是索引数组的列索引,或者是关联数组的列的键,也可以是属性名。
也可以是 &null; ,此时将返回整个数组(配合
<parameter>index_key</parameter> 参数来重新索引数组时非常好用)。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>index_key</parameter></term>
<listitem>
<para>
作为返回数组的索引/键的列。它可以是该列的整数索引,或者字符串键值。
该值会像数组键一样被 <link linkend="language.types.array.key-casts">强制转换</link>
(但是,在 PHP 8.0.0 之前,也被允许支持转换为字符串对象)。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
返回输入数组中单列值的数组。
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>8.0.0</entry>
<entry>
<parameter>index_key</parameter> 参数指定的列中的对象不再强制转换为字符串,而是会抛出
<classname>TypeError</classname>。
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>从结果集中取出 first_name 列</title>
<programlisting role="php">
<![CDATA[
<?php
// 表示从数据库返回的记录集的数组
$records = [
[
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe',
],
[
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith',
],
[
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones',
],
[
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe',
]
];
$first_names = array_column($records, 'first_name');
print_r($first_names);
?>
]]>
</programlisting>
&example.outputs;
<screen role="php">
<![CDATA[
Array
(
[0] => John
[1] => Sally
[2] => Jane
[3] => Peter
)
]]>
</screen>
</example>
</para>
<para>
<example>
<title>
从结果集中总取出 last_name 列,用相应的“id”作为键值
</title>
<programlisting role="php">
<![CDATA[
<?php
// 使用示例 #1 中的 $records 数组
$records = [
[
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe',
],
[
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith',
],
[
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones',
],
[
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe',
]
];
$last_names = array_column($records, 'last_name', 'id');
print_r($last_names);
?>
]]>
</programlisting>
&example.outputs;
<screen role="php">
<![CDATA[
Array
(
[2135] => Doe
[3245] => Smith
[5342] => Jones
[5623] => Doe
)
]]>
</screen>
</example>
</para>
<para>
<example>
<title>
username 列是从对象获取 public 的 "username" 属性
</title>
<programlisting role="php">
<![CDATA[
<?php
class User
{
public $username;
public function __construct(string $username)
{
$this->username = $username;
}
}
$users = [
new User('user 1'),
new User('user 2'),
new User('user 3'),
];
print_r(array_column($users, 'username'));
?>
]]>
</programlisting>
&example.outputs;
<screen role="php">
<![CDATA[
Array
(
[0] => user 1
[1] => user 2
[2] => user 3
)
]]>
</screen>
</example>
</para>
<para>
<example>
<title>
通过 <function>__isset</function> 和 <function>__get</function> 魔术方法从对象中获取 private 属性的 "name" 列。
</title>
<programlisting role="php">
<![CDATA[
<?php
class Person
{
private $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function __get($prop)
{
return $this->$prop;
}
public function __isset($prop) : bool
{
return isset($this->$prop);
}
}
$people = [
new Person('Fred'),
new Person('Jane'),
new Person('John'),
];
print_r(array_column($people, 'name'));
?>
]]>
</programlisting>
&example.outputs;
<screen role="php">
<![CDATA[
Array
(
[0] => Fred
[1] => Jane
[2] => John
)
]]>
</screen>
</example>
如果不提供 <function>__isset</function>,会返回空数组。
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->