1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-24 07:02:15 +01:00
Files
archived-doc-zh/reference/array/functions/array-column.xml
2024-12-31 08:47:57 +08:00

319 lines
7.1 KiB
XML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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
-->