mirror of
https://github.com/php/doc-zh.git
synced 2026-03-24 15:12:20 +01:00
159 lines
4.3 KiB
XML
Executable File
159 lines
4.3 KiB
XML
Executable File
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- EN-Revision: cd943f94a013b74df8765ab8e1a620a916a64a85 Maintainer: daijie Status: ready -->
|
|
<refentry xml:id="function.compact" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>compact</refname>
|
|
<refpurpose>
|
|
建立一个数组,包括变量名和它们的值
|
|
</refpurpose>
|
|
</refnamediv>
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type>array</type><methodname>compact</methodname>
|
|
<methodparam><type class="union"><type>array</type><type>string</type></type><parameter>var_name</parameter></methodparam>
|
|
<methodparam rep="repeat"><type class="union"><type>array</type><type>string</type></type><parameter>var_names</parameter></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
创建一个包含变量与其值的数组。
|
|
</para>
|
|
<para>
|
|
对每个参数,<function>compact</function>
|
|
在当前的符号表中查找该变量名并将它添加到输出的数组中,变量名成为键名而变量的内容成为该键的值。简单说,它做的事和
|
|
<function>extract</function> 正好相反。返回将所有变量添加进去后的数组。
|
|
</para>
|
|
<note>
|
|
<para>
|
|
在 PHP 7.3 之前版本,未设置的字符串会被静默忽略。
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>var_name</parameter></term>
|
|
<term><parameter>var_names</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
<function>compact</function> 接受可变的参数数目。每个参数可以是一个包括变量名的字符串或者是一个包含变量名的数组,该数组中还可以包含其它单元内容为变量名的数组,
|
|
<function>compact</function> 可以递归处理。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
返回输出的数组,包含了添加的所有变量。
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="errors">
|
|
&reftitle.errors;
|
|
<para>
|
|
如果字符串指向的变量未定义,<function>compact</function> 会产生 E_NOTICE 级错误。
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="changelog"><!-- {{{ -->
|
|
&reftitle.changelog;
|
|
<informaltable>
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>&Version;</entry>
|
|
<entry>&Description;</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>7.3.0</entry>
|
|
<entry>
|
|
现在,如果字符串指向的变量未定义,<function>compact</function> 会产生 E_NOTICE 级错误。
|
|
在此之前,此类问题会静默忽略掉。
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</refsect1><!-- }}} -->
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title><function>compact</function> 例子</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$city = "San Francisco";
|
|
$state = "CA";
|
|
$event = "SIGGRAPH";
|
|
|
|
$location_vars = array("city", "state");
|
|
|
|
$result = compact("event", $location_vars);
|
|
print_r($result);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen role="php">
|
|
<![CDATA[
|
|
Array
|
|
(
|
|
[event] => SIGGRAPH
|
|
[city] => San Francisco
|
|
[state] => CA
|
|
)
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
<refsect1 role="notes">
|
|
&reftitle.notes;
|
|
<note>
|
|
<title>Gotcha</title>
|
|
<para>
|
|
因为<link linkend="language.variables.variable">可变变量</link>也许不能在函数内部用于
|
|
PHP 的<link linkend="language.variables.superglobals">超全局数组</link>,此时不能将超全局数组传递入
|
|
<function>compact</function> 中。
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>extract</function></member>
|
|
</simplelist>
|
|
</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
|
|
-->
|