reference/array/functions/array-column.xml
8bf3587d8f70239a65d9aa44d42ced8a696a3e86
8bf3587d8f70239a65d9aa44d42ced8a696a3e86
...
...
@@ -9,13 +9,13 @@
9
9
&reftitle.description;
10
10
<methodsynopsis>
11
11
<type>array</type><methodname>array_column</methodname>
12
-
<methodparam><type>array</type><parameter>input</parameter></methodparam>
13
-
<methodparam><type>mixed</type><parameter>column_key</parameter></methodparam>
14
-
<methodparam choice="opt"><type>mixed</type><parameter>index_key</parameter><initializer>&null;</initializer></methodparam>
12
+
<methodparam><type>array</type><parameter>array</parameter></methodparam>
13
+
<methodparam><type class="union"><type>int</type><type>string</type><type>null</type></type><parameter>column_key</parameter></methodparam>
14
+
<methodparam choice="opt"><type class="union"><type>int</type><type>string</type><type>null</type></type><parameter>index_key</parameter><initializer>&null;</initializer></methodparam>
15
15
</methodsynopsis>
16
16
<para>
17
17
<function>array_column</function> returns the values from a single column of
18
-
the <parameter>input</parameter>, identified by the
18
+
the <parameter>array</parameter>, identified by the
19
19
<parameter>column_key</parameter>. Optionally, an
20
20
<parameter>index_key</parameter> may be provided to index the values in the
21
21
returned array by the values from the <parameter>index_key</parameter>
...
...
@@ -27,7 +27,7 @@
27
27
<para>
28
28
<variablelist>
29
29
<varlistentry>
30
-
<term><parameter>input</parameter></term>
30
+
<term><parameter>array</parameter></term>
31
31
<listitem>
32
32
<para>
33
33
A multi-dimensional array or an array of objects from which to pull a
...
...
@@ -58,8 +58,8 @@
58
58
The column to use as the index/keys for the returned array. This value
59
59
may be the integer key of the column, or it may be the string key name.
60
60
The value is <link linkend="language.types.array.key-casts">cast</link>
61
-
as usual for array keys (however, objects supporting conversion to string
62
-
are also allowed).
61
+
as usual for array keys (however, prior to PHP 8.0.0, objects supporting
62
+
conversion to string were also allowed).
63
63
</para>
64
64
</listitem>
65
65
</varlistentry>
...
...
@@ -85,10 +85,10 @@
85
85
</thead>
86
86
<tbody>
87
87
<row>
88
-
<entry>7.0.0</entry>
88
+
<entry>8.0.0</entry>
89
89
<entry>
90
-
Added the ability for the <parameter>input</parameter> parameter to be
91
-
an array of objects.
90
+
Objects in columns indicated by <parameter>index_key</parameter> parameter
91
+
will no longer be cast to string and will now throw a <classname>TypeError</classname> instead.
92
92
</entry>
93
93
</row>
94
94
</tbody>
...
...
@@ -104,32 +104,34 @@
104
104
<programlisting role="php">
105
105
<![CDATA[
106
106
<?php
107
+
107
108
// Array representing a possible record set returned from a database
108
-
$records = array(
109
-
array(
109
+
$records = [
110
+
[
110
111
'id' => 2135,
111
112
'first_name' => 'John',
112
113
'last_name' => 'Doe',
113
-
),
114
-
array(
114
+
],
115
+
[
115
116
'id' => 3245,
116
117
'first_name' => 'Sally',
117
118
'last_name' => 'Smith',
118
-
),
119
-
array(
119
+
],
120
+
[
120
121
'id' => 5342,
121
122
'first_name' => 'Jane',
122
123
'last_name' => 'Jones',
123
-
),
124
-
array(
124
+
],
125
+
[
125
126
'id' => 5623,
126
127
'first_name' => 'Peter',
127
128
'last_name' => 'Doe',
128
-
)
129
-
);
129
+
]
130
+
];
130
131
131
132
$first_names = array_column($records, 'first_name');
132
133
print_r($first_names);
134
+
133
135
?>
134
136
]]>
135
137
</programlisting>
...
...
@@ -155,9 +157,34 @@ Array
155
157
<programlisting role="php">
156
158
<![CDATA[
157
159
<?php
160
+
158
161
// Using the $records array from Example #1
162
+
$records = [
163
+
[
164
+
'id' => 2135,
165
+
'first_name' => 'John',
166
+
'last_name' => 'Doe',
167
+
],
168
+
[
169
+
'id' => 3245,
170
+
'first_name' => 'Sally',
171
+
'last_name' => 'Smith',
172
+
],
173
+
[
174
+
'id' => 5342,
175
+
'first_name' => 'Jane',
176
+
'last_name' => 'Jones',
177
+
],
178
+
[
179
+
'id' => 5623,
180
+
'first_name' => 'Peter',
181
+
'last_name' => 'Doe',
182
+
]
183
+
];
184
+
159
185
$last_names = array_column($records, 'last_name', 'id');
160
186
print_r($last_names);
187
+
161
188
?>
162
189
]]>
163
190
</programlisting>
...
...
@@ -202,6 +229,7 @@ $users = [
202
229
];
203
230
204
231
print_r(array_column($users, 'username'));
232
+
205
233
?>
206
234
]]>
207
235
</programlisting>
...
...
@@ -222,7 +250,7 @@ Array
222
250
<example>
223
251
<title>
224
252
Get the column of names from the private "name" property of an object
225
-
using the magic <function>__get</function> method.
253
+
using the magic methods <function>__isset</function> and <function>__get</function>
226
254
</title>
227
255
<programlisting role="php">
228
256
<![CDATA[
...
...
@@ -275,20 +303,7 @@ Array
275
303
</para>
276
304
</refsect1>
277
305
278
-
<refsect1 role="seealso">
279
-
&reftitle.seealso;
280
-
<para>
281
-
<simplelist>
282
-
<member>
283
-
<link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="&url.arraycolumn.compat;">Recommended
284
-
userland implementation for PHP lower than 5.5</link>
285
-
</member>
286
-
</simplelist>
287
-
</para>
288
-
</refsect1>
289
-
290
306
</refentry>
291
-
292
307
<!-- Keep this comment at the end of the file
293
308
Local variables:
294
309
mode: sgml
295
310