1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-25 23:52:17 +01:00
Files
archived-doc-zh/reference/array/functions/list.xml
Dai Jie 264ee58391 Sync array functions .
git-svn-id: https://svn.php.net/repository/phpdoc/zh/trunk@327980 c90b9560-bf6c-de11-be94-00142212c4b1
2012-10-10 12:54:58 +00:00

214 lines
4.8 KiB
XML
Executable File

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- $Author$ -->
<!-- EN-Revision: 123babdf91f07356296ee7cf0c21579581438fe5 Maintainer: dallas Status: ready -->
<refentry xml:id="function.list" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>list</refname>
<refpurpose>
把数组中的值赋给一些变量
</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>array</type><methodname>list</methodname>
<methodparam><type>mixed</type><parameter>varname</parameter></methodparam>
<methodparam rep="repeat" choice="opt"><type>mixed</type><parameter>...</parameter></methodparam>
</methodsynopsis>
<para>
<function>array</function> 一样,这不是真正的函数,而是语言结构。<function>list</function>
用一步操作给一组变量进行赋值。
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>varname</parameter></term>
<listitem>
<para>
一个变量。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
返回指定的数组。
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>list</function> 例子</title>
<programlisting role="php">
<![CDATA[
<?php
$info = array('coffee', 'brown', 'caffeine');
// 列出所有变量
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n";
// 列出他们的其中一个
list($drink, , $power) = $info;
echo "$drink has $power.\n";
// 或者让我们跳到仅第三个
list( , , $power) = $info;
echo "I need $power!\n";
// list() 不能对字符串起作用
list($bar) = "abcde";
var_dump($bar); // NULL
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title><function>list</function> 用法的一个例子</title>
<programlisting role="php">
<![CDATA[
<table>
<tr>
<th>Employee name</th>
<th>Salary</th>
</tr>
<?php
$result = mysql_query("SELECT id, name, salary FROM employees", $conn);
while (list($id, $name, $salary) = mysql_fetch_row($result)) {
echo " <tr>\n" .
" <td><a href=\"info.php?id=$id\">$name</a></td>\n" .
" <td>$salary</td>\n" .
" </tr>\n";
}
?>
</table>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>使用嵌套的 <function>list</function></title>
<programlisting role="php">
<![CDATA[
<?php
list($a, list($b, $c)) = array(1, array(2, 3));
var_dump($a, $b, $c);
?>
]]>
</programlisting>
<screen>
<![CDATA[
int(1)
int(2)
int(3)
]]>
</screen>
</example>
</para>
<para>
<example>
<title><function>list</function> 中使用数组索引</title>
<programlisting role="php">
<![CDATA[
<?php
$info = array('coffee', 'brown', 'caffeine');
list($a[0], $a[1], $a[2]) = $info;
var_dump($a);
?>
]]>
</programlisting>
<para>
产生如下输出(注意单元顺序和 <function>list</function> 语法中所写的顺序的比较):
</para>
<screen>
<![CDATA[
array(3) {
[2]=>
string(8) "caffeine"
[1]=>
string(5) "brown"
[0]=>
string(6) "coffee"
}
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<warning>
<para>
<function>list</function> 从最右边一个参数开始赋值。如果你用单纯的变量,不用担心这一点。
但是如果你用了具有索引的数组,通常你期望得到的结果和在
<function>list</function> 中写的一样是从左到右的,但实际上不是。
它是以相反顺序赋值的。
</para>
</warning>
<warning>
<para>
<function>list</function> 执行过程中修改数组(比如使用 <literal>list($a, $b) = $b</literal>)将会产生不可预知的结果。
</para>
</warning>
<note>
<para>
<function>list</function> 仅能用于数字索引的数组并假定数字索引从 0 开始。
</para>
</note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>each</function></member>
<member><function>array</function></member>
<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
-->