Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 10724

php-manual-zh-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>数组</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="PHP 手册"
HREF="index.html"><LINK
REL="UP"
TITLE="类型"
HREF="language.types.html"><LINK
REL="PREVIOUS"
TITLE="字符串"
HREF="language.types.string.html"><LINK
REL="NEXT"
TITLE="对象"
HREF="language.types.object.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>PHP 手册</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="language.types.string.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>章 11. 类型</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.types.object.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.types.array"
>数组</A
></H1
><P
>&#13;   PHP 中的数组实际上是一个有序图。图是一种把 <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>values</I
></SPAN
>
   映射到 <SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>keys</I
></SPAN
> 的类型。此类型在很多方面做了优化,因此可以把它当成真正的数组来使用,或列表(矢量),散列表(是图的一种实现),字典,集合,栈,队列以及更多可能性。因为可以用另一个
   PHP 数组作为值,也可以很容易地模拟树。
  </P
><P
>&#13;   解释这些结构超出了本手册的范围,但对于每种结构至少会发现一个例子。要得到这些结构的更多信息,建议参考有关此广阔主题的外部著作。
  </P
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.array.syntax"
>语法</A
></H2
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="language.types.array.syntax.array-func"
>定义 <A
HREF="function.array.html"
><B
CLASS="function"
>array()</B
></A
></A
></H3
><P
>&#13;     可以用 <A
HREF="function.array.html"
><B
CLASS="function"
>array()</B
></A
> 语言结构来新建一个 <A
HREF="language.types.array.html"
><B
CLASS="type"
>array</B
></A
>。它接受一定数量用逗号分隔的
     <TT
CLASS="literal"
><TT
CLASS="replaceable"
><I
>key</I
></TT
> =&#62; <TT
CLASS="replaceable"
><I
>value</I
></TT
></TT
> 参数对。
    </P
><P
>&#13;     <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="synopsis"
>array( [<TT
CLASS="replaceable"
><I
>key</I
></TT
> =&#62;]
<TT
CLASS="replaceable"
><I
>value</I
></TT
>
     , ...
     )
// <TT
CLASS="replaceable"
><I
>key</I
></TT
> 可以是 <A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
> 或者 <A
HREF="language.types.string.html"
><B
CLASS="type"
>string</B
></A
>
// <TT
CLASS="replaceable"
><I
>value</I
></TT
> 可以是任何值</PRE
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3182"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$arr </font><font color="#007700">= array(</font><font color="#DD0000">"foo" </font><font color="#007700">=&gt; </font><font color="#DD0000">"bar"</font><font color="#007700">, </font><font color="#0000BB">12 </font><font color="#007700">=&gt; </font><font color="#0000BB">true</font><font color="#007700">);<br /><br />echo </font><font color="#0000BB">$arr</font><font color="#007700">[</font><font color="#DD0000">"foo"</font><font color="#007700">]; </font><font color="#FF8000">// bar<br /></font><font color="#007700">echo </font><font color="#0000BB">$arr</font><font color="#007700">[</font><font color="#0000BB">12</font><font color="#007700">];&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 1<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     <CODE
CLASS="varname"
>key</CODE
> 可以是 <A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
> 或者 <A
HREF="language.types.string.html"
><B
CLASS="type"
>string</B
></A
>。如果键名是一个
     <A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
> 的标准表达方法,则被解释为整数(例如 <TT
CLASS="literal"
>"8"</TT
> 将被解释为
     <TT
CLASS="literal"
>8</TT
>,而 <TT
CLASS="literal"
>"08"</TT
> 将被解释为 <TT
CLASS="literal"
>"08"</TT
>)。<CODE
CLASS="varname"
>key</CODE
>
     中的浮点数被取整为 <A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
>。PHP
     中没有不同的数字下标和关联下标数组,数组的类型只有一种,它可以同时包含整型和字符串型的下标。
    </P
><P
>&#13;     值可以是任何值。
     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3196"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$arr </font><font color="#007700">= array(</font><font color="#DD0000">"somearray" </font><font color="#007700">=&gt; array(</font><font color="#0000BB">6 </font><font color="#007700">=&gt; </font><font color="#0000BB">5</font><font color="#007700">, </font><font color="#0000BB">13 </font><font color="#007700">=&gt; </font><font color="#0000BB">9</font><font color="#007700">, </font><font color="#DD0000">"a" </font><font color="#007700">=&gt; </font><font color="#0000BB">42</font><font color="#007700">));<br /><br />echo </font><font color="#0000BB">$arr</font><font color="#007700">[</font><font color="#DD0000">"somearray"</font><font color="#007700">][</font><font color="#0000BB">6</font><font color="#007700">];&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 5<br /></font><font color="#007700">echo </font><font color="#0000BB">$arr</font><font color="#007700">[</font><font color="#DD0000">"somearray"</font><font color="#007700">][</font><font color="#0000BB">13</font><font color="#007700">];&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// 9<br /></font><font color="#007700">echo </font><font color="#0000BB">$arr</font><font color="#007700">[</font><font color="#DD0000">"somearray"</font><font color="#007700">][</font><font color="#DD0000">"a"</font><font color="#007700">];&nbsp;&nbsp;</font><font color="#FF8000">// 42<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     如果对给出的值没有指定键名,则取当前最大的整数索引值,而新的键名将是该值加一。如果指定的键名已经有了值,则该值会被覆盖。
     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3199"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// This array is the same as ...<br /></font><font color="#007700">array(</font><font color="#0000BB">5 </font><font color="#007700">=&gt; </font><font color="#0000BB">43</font><font color="#007700">, </font><font color="#0000BB">32</font><font color="#007700">, </font><font color="#0000BB">56</font><font color="#007700">, </font><font color="#DD0000">"b" </font><font color="#007700">=&gt; </font><font color="#0000BB">12</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// ...this array<br /></font><font color="#007700">array(</font><font color="#0000BB">5 </font><font color="#007700">=&gt; </font><font color="#0000BB">43</font><font color="#007700">, </font><font color="#0000BB">6 </font><font color="#007700">=&gt; </font><font color="#0000BB">32</font><font color="#007700">, </font><font color="#0000BB">7 </font><font color="#007700">=&gt; </font><font color="#0000BB">56</font><font color="#007700">, </font><font color="#DD0000">"b" </font><font color="#007700">=&gt; </font><font color="#0000BB">12</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>警告</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>&#13;      自 PHP 4.3.0 起,上述的索引生成方法改变了。如今如果给一个当前最大键名是负值的数组添加一个新值,则新生成的的索引将为零(<TT
CLASS="literal"
>0</TT
>)。以前新生成的索引为当前最大索引加一,和正值的索引相同。
     </P
></TD
></TR
></TABLE
></DIV
><P
>&#13;     使用 <TT
CLASS="constant"
><B
>TRUE</B
></TT
> 作为键名将使 <A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
> <TT
CLASS="literal"
>1</TT
> 成为键名。使用
     <TT
CLASS="constant"
><B
>FALSE</B
></TT
> 作为键名将使 <A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
> <TT
CLASS="literal"
>0</TT
> 成为键名。使用
     <TT
CLASS="literal"
>NULL</TT
> 作为键名将等同于使用空字符串。使用空字符串作为键名将新建(或覆盖)一个用空字符串作为键名的值,这和用空的方括号不一样。
    </P
><P
>&#13;     不能用数组和对象作为键名。这样做会导致一个警告:<TT
CLASS="literal"
>Illegal offset type</TT
>。
    </P
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="language.types.array.syntax.modifying"
>用方括号的语法新建/修改</A
></H3
><P
>&#13;     可以通过明示地设定值来改变一个现有的数组。
    </P
><P
>&#13;     这是通过在方括号内指定键名来给数组赋值实现的。也可以省略键名,在这种情况下给变量名加上一对空的方括号(“<TT
CLASS="literal"
>[]</TT
>”)。
     <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="synopsis"
>$arr[<TT
CLASS="replaceable"
><I
>key</I
></TT
>] = <TT
CLASS="replaceable"
><I
>value</I
></TT
>;
$arr[] = <TT
CLASS="replaceable"
><I
>value</I
></TT
>;
// <TT
CLASS="replaceable"
><I
>key</I
></TT
> 可以是 <A
HREF="language.types.integer.html"
><B
CLASS="type"
>integer</B
></A
> 或者 <A
HREF="language.types.string.html"
><B
CLASS="type"
>string</B
></A
>
// <TT
CLASS="replaceable"
><I
>value</I
></TT
> 可以为任何值。</PRE
></TD
></TR
></TABLE
>
     如果 <CODE
CLASS="varname"
>$arr</CODE
> 还不存在,将会新建一个。这也是一种定义数组的替换方法。要改变一个值,只要给它赋一个新值。如果要删除一个键名/值对,要对它用 <A
HREF="function.unset.html"
><B
CLASS="function"
>unset()</B
></A
>。
     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3229"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$arr </font><font color="#007700">= array(</font><font color="#0000BB">5 </font><font color="#007700">=&gt; </font><font color="#0000BB">1</font><font color="#007700">, </font><font color="#0000BB">12 </font><font color="#007700">=&gt; </font><font color="#0000BB">2</font><font color="#007700">);<br /></font><font color="#0000BB">$arr</font><font color="#007700">[] = </font><font color="#0000BB">56</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// This is the same as $arr[13] = 56;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// at this point of the script<br /></font><font color="#0000BB">$arr</font><font color="#007700">[</font><font color="#DD0000">"x"</font><font color="#007700">] = </font><font color="#0000BB">42</font><font color="#007700">; </font><font color="#FF8000">// This adds a new element to<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// the array with key "x"<br /></font><font color="#007700">unset(</font><font color="#0000BB">$arr</font><font color="#007700">[</font><font color="#0000BB">5</font><font color="#007700">]); </font><font color="#FF8000">// This removes the element from the array<br /></font><font color="#007700">unset(</font><font color="#0000BB">$arr</font><font color="#007700">);&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// This deletes the whole array<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
      如上所述,如果给出方括号但没有指定键名,则取当前最大整数索引值,新的键名将是该值
      + 1。如果当前还没有整数索引,则键名将为
      <TT
CLASS="literal"
>0</TT
>。如果指定的键名已经有值了,该值将被覆盖。
     </P
><P
>&#13;      <DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>警告</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>&#13;        自 PHP 4.3.0 起,上述的索引生成方法改变了。如今如果给一个当前最大键名是负值的数组添加一个新值,则新生成的的索引将为零(<TT
CLASS="literal"
>0</TT
>)。以前新生成的索引为当前最大索引加一,和正值的索引相同。
       </P
></TD
></TR
></TABLE
></DIV
>
     </P
><P
>&#13;      注意这里所使用的最大整数键名<SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>不一定</I
></SPAN
>当前就在数组中。它只要在上次数组重新生成索引后曾经存在过就行了。以下面的例子来说明:
     </P
><DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3240"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// 创建一个简单的数组<br /></font><font color="#0000BB">$array </font><font color="#007700">= array(</font><font color="#0000BB">1</font><font color="#007700">, </font><font color="#0000BB">2</font><font color="#007700">, </font><font color="#0000BB">3</font><font color="#007700">, </font><font color="#0000BB">4</font><font color="#007700">, </font><font color="#0000BB">5</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$array</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// 现在删除其中的所有单元,但保持数组本身的结构<br /></font><font color="#007700">foreach (</font><font color="#0000BB">$array </font><font color="#007700">as </font><font color="#0000BB">$i </font><font color="#007700">=&gt; </font><font color="#0000BB">$value</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;unset(</font><font color="#0000BB">$array</font><font color="#007700">[</font><font color="#0000BB">$i</font><font color="#007700">]);<br />}<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$array</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// 添加一个单元(注意新的键名是 5,而不是你可能以为的 0)<br /></font><font color="#0000BB">$array</font><font color="#007700">[] = </font><font color="#0000BB">6</font><font color="#007700">;<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$array</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// 重新索引:<br /></font><font color="#0000BB">$array </font><font color="#007700">= </font><font color="#0000BB">array_values</font><font color="#007700">(</font><font color="#0000BB">$array</font><font color="#007700">);<br /></font><font color="#0000BB">$array</font><font color="#007700">[] = </font><font color="#0000BB">7</font><font color="#007700">;<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$array</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>上例将输出:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Array
(
    [0] =&#62; 1
    [1] =&#62; 2
    [2] =&#62; 3
    [3] =&#62; 4
    [4] =&#62; 5
)
Array
(
)
Array
(
    [5] =&#62; 6
)
Array
(
    [0] =&#62; 6
    [1] =&#62; 7
)</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
></BLOCKQUOTE
></DIV
></DIV
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.array.useful-funcs"
>实用函数</A
></H2
><P
>&#13;    有相当多的实用函数作用于数组,参见<A
HREF="ref.array.html"
>数组函数</A
>一节。
   </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
     <A
HREF="function.unset.html"
><B
CLASS="function"
>unset()</B
></A
> 函数允许取消一个数组中的键名。要注意数组将不会重建索引。
     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3251"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?PHP<br />$a </font><font color="#007700">= array( </font><font color="#0000BB">1 </font><font color="#007700">=&gt; </font><font color="#DD0000">'one'</font><font color="#007700">, </font><font color="#0000BB">2 </font><font color="#007700">=&gt; </font><font color="#DD0000">'two'</font><font color="#007700">, </font><font color="#0000BB">3 </font><font color="#007700">=&gt; </font><font color="#DD0000">'three' </font><font color="#007700">);<br />unset( </font><font color="#0000BB">$a</font><font color="#007700">[</font><font color="#0000BB">2</font><font color="#007700">] );<br /></font><font color="#FF8000">/* 将产生一个数组,定义为<br />&nbsp;&nbsp;&nbsp;$a = array( 1=&gt;'one', 3=&gt;'three');<br />&nbsp;&nbsp;&nbsp;而不是<br />&nbsp;&nbsp;&nbsp;$a = array( 1 =&gt; 'one', 2 =&gt; 'three');<br />*/<br /></font><font color="#0000BB">$b </font><font color="#007700">= </font><font color="#0000BB">array_values</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">);<br /></font><font color="#FF8000">// Now $b is array(0 =&gt; 'one', 1 =&gt;'three')<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
></BLOCKQUOTE
></DIV
><P
>&#13;    <A
HREF="control-structures.foreach.html"
>foreach</A
> 控制结构是专门用于数组的。它提供了一个简单的方法来遍历数组。
   </P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.array.donts"
>数组做什么和不做什么</A
></H2
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="language.types.array.foo-bar"
>为什么 <TT
CLASS="literal"
>$foo[bar]</TT
> 错了?</A
></H3
><P
>&#13;     应该始终在用字符串表示的数组索引上加上引号。例如用
     $foo['bar'] 而不是 $foo[bar]。但是为什么 $foo[bar] 错了呢?可能在老的脚本中见过如下语法:
     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3261"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$foo</font><font color="#007700">[</font><font color="#0000BB">bar</font><font color="#007700">] = </font><font color="#DD0000">'enemy'</font><font color="#007700">;<br />echo </font><font color="#0000BB">$foo</font><font color="#007700">[</font><font color="#0000BB">bar</font><font color="#007700">];<br /></font><font color="#FF8000">// etc<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
     这样是错的,但可以正常运行。那么为什么错了呢?原因是此代码中有一个未定义的常量(bar)而不是字符串('bar'-注意引号),而 PHP 可能会在以后定义此常量,不幸的是你的代码中有同样的名字。它能运行,是因为
     PHP 自动将<SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>裸字符串</I
></SPAN
>(没有引号的字符串且不对应于任何已知符号)转换成一个其值为该裸字符串的正常字符串。例如,如果没有常量定义为
     <TT
CLASS="constant"
><B
>bar</B
></TT
>,PHP 将把它替代为 <TT
CLASS="literal"
>'bar'</TT
> 并使用之。
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
      这并不意味着<SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>总是</I
></SPAN
>给键名加上引号。用不着给键名为<A
HREF="language.constants.html"
>常量</A
>或<A
HREF="language.variables.html"
>变量</A
>的加上引号,否则会使 PHP
      不能解析它们。
     </P
><DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3271"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />error_reporting</font><font color="#007700">(</font><font color="#0000BB">E_ALL</font><font color="#007700">);<br /></font><font color="#0000BB">ini_set</font><font color="#007700">(</font><font color="#DD0000">'display_errors'</font><font color="#007700">, </font><font color="#0000BB">true</font><font color="#007700">);<br /></font><font color="#0000BB">ini_set</font><font color="#007700">(</font><font color="#DD0000">'html_errors'</font><font color="#007700">, </font><font color="#0000BB">false</font><font color="#007700">);<br /></font><font color="#FF8000">// Simple array:<br /></font><font color="#0000BB">$array </font><font color="#007700">= array(</font><font color="#0000BB">1</font><font color="#007700">, </font><font color="#0000BB">2</font><font color="#007700">);<br /></font><font color="#0000BB">$count </font><font color="#007700">= </font><font color="#0000BB">count</font><font color="#007700">(</font><font color="#0000BB">$array</font><font color="#007700">);<br />for (</font><font color="#0000BB">$i </font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">; </font><font color="#0000BB">$i </font><font color="#007700">&lt; </font><font color="#0000BB">$count</font><font color="#007700">; </font><font color="#0000BB">$i</font><font color="#007700">++) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"</font><font color="#007700">\n</font><font color="#DD0000">Checking $i: </font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Bad: " </font><font color="#007700">. </font><font color="#0000BB">$array</font><font color="#007700">[</font><font color="#DD0000">'$i'</font><font color="#007700">] . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Good: " </font><font color="#007700">. </font><font color="#0000BB">$array</font><font color="#007700">[</font><font color="#0000BB">$i</font><font color="#007700">] . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Bad: </font><font color="#007700">{</font><font color="#DD0000">$array</font><font color="#007700">[</font><font color="#DD0000">'$i'</font><font color="#007700">]}\n</font><font color="#DD0000">"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Good: </font><font color="#007700">{</font><font color="#DD0000">$array</font><font color="#007700">[</font><font color="#DD0000">$i</font><font color="#007700">]}\n</font><font color="#DD0000">"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
><P
><B
>注意: </B
>上例的输出类似于:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Checking 0:
Notice: Undefined index:  $i in /path/to/script.php on line 10
Bad:
Good: 1
Notice: Undefined index:  $i in /path/to/script.php on line 12
Bad:
Good: 1

Checking 1:
Notice: Undefined index:  $i in /path/to/script.php on line 10
Bad:
Good: 2
Notice: Undefined index:  $i in /path/to/script.php on line 12
Bad:
Good: 2</PRE
></TD
></TR
></TABLE
></BLOCKQUOTE
></DIV
><P
>&#13;     演示此效应的更多例子:
     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3276"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// 显示所有错误<br /></font><font color="#0000BB">error_reporting</font><font color="#007700">(</font><font color="#0000BB">E_ALL</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$arr </font><font color="#007700">= array(</font><font color="#DD0000">'fruit' </font><font color="#007700">=&gt; </font><font color="#DD0000">'apple'</font><font color="#007700">, </font><font color="#DD0000">'veggie' </font><font color="#007700">=&gt; </font><font color="#DD0000">'carrot'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// 正确<br /></font><font color="#007700">print </font><font color="#0000BB">$arr</font><font color="#007700">[</font><font color="#DD0000">'fruit'</font><font color="#007700">];&nbsp;&nbsp;</font><font color="#FF8000">// apple<br /></font><font color="#007700">print </font><font color="#0000BB">$arr</font><font color="#007700">[</font><font color="#DD0000">'veggie'</font><font color="#007700">]; </font><font color="#FF8000">// carrot<br /><br />// 不正确。This works but also throws a PHP error of<br />// level E_NOTICE because of an undefined constant named fruit<br />//<br />// Notice: Use of undefined constant fruit - assumed 'fruit' in...<br /></font><font color="#007700">print </font><font color="#0000BB">$arr</font><font color="#007700">[</font><font color="#0000BB">fruit</font><font color="#007700">];&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// apple<br /><br />// Let's define a constant to demonstrate what's going on.&nbsp;&nbsp;We<br />// will assign value 'veggie' to a constant named fruit.<br /></font><font color="#0000BB">define</font><font color="#007700">(</font><font color="#DD0000">'fruit'</font><font color="#007700">,</font><font color="#DD0000">'veggie'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// Notice the difference now<br /></font><font color="#007700">print </font><font color="#0000BB">$arr</font><font color="#007700">[</font><font color="#DD0000">'fruit'</font><font color="#007700">];&nbsp;&nbsp;</font><font color="#FF8000">// apple<br /></font><font color="#007700">print </font><font color="#0000BB">$arr</font><font color="#007700">[</font><font color="#0000BB">fruit</font><font color="#007700">];&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// carrot<br /><br />// The following is okay as it's inside a string.&nbsp;&nbsp;Constants are not<br />// looked for within strings so no E_NOTICE error here<br /></font><font color="#007700">print </font><font color="#DD0000">"Hello $arr</font><font color="#007700">[</font><font color="#DD0000">fruit</font><font color="#007700">]</font><font color="#DD0000">"</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// Hello apple<br /><br />// With one exception, braces surrounding arrays within strings<br />// allows constants to be looked for<br /></font><font color="#007700">print </font><font color="#DD0000">"Hello </font><font color="#007700">{</font><font color="#DD0000">$arr</font><font color="#007700">[</font><font color="#DD0000">fruit</font><font color="#007700">]}</font><font color="#DD0000">"</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// Hello carrot<br /></font><font color="#007700">print </font><font color="#DD0000">"Hello </font><font color="#007700">{</font><font color="#DD0000">$arr</font><font color="#007700">[</font><font color="#DD0000">'fruit'</font><font color="#007700">]}</font><font color="#DD0000">"</font><font color="#007700">;&nbsp;&nbsp;</font><font color="#FF8000">// Hello apple<br /><br />// This will not work, results in a parse error such as:<br />// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'<br />// This of course applies to using autoglobals in strings as well<br /></font><font color="#007700">print </font><font color="#DD0000">"Hello $arr</font><font color="#007700">[</font><font color="#DD0000">'fruit'</font><font color="#007700">]</font><font color="#DD0000">"</font><font color="#007700">;<br />print </font><font color="#DD0000">"Hello $_GET</font><font color="#007700">[</font><font color="#DD0000">'foo'</font><font color="#007700">]</font><font color="#DD0000">"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// Concatenation is another option<br /></font><font color="#007700">print </font><font color="#DD0000">"Hello " </font><font color="#007700">. </font><font color="#0000BB">$arr</font><font color="#007700">[</font><font color="#DD0000">'fruit'</font><font color="#007700">]; </font><font color="#FF8000">// Hello apple<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
    </P
><P
>&#13;     当打开 <A
HREF="function.error-reporting.html"
><B
CLASS="function"
>error_reporting()</B
></A
> 来显示
     <TT
CLASS="constant"
><B
>E_NOTICE</B
></TT
> 级别的错误(例如将其设为
     <TT
CLASS="constant"
><B
>E_ALL</B
></TT
>)时将看到这些错误。默认情况下
     <A
HREF="ref.errorfunc.html#ini.error-reporting"
>error_reporting</A
>
     被关闭不显示这些。
    </P
><P
>&#13;     和在<A
HREF="language.types.array.html#language.types.array.syntax"
>语法</A
>一节中规定的一样,在方括号(“<TT
CLASS="literal"
>[</TT
>”和“<TT
CLASS="literal"
>]</TT
>”)之间必须有一个表达式。这意味着可以这样写:
     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3287"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">echo </font><font color="#0000BB">$arr</font><font color="#007700">[</font><font color="#0000BB">somefunc</font><font color="#007700">(</font><font color="#0000BB">$bar</font><font color="#007700">)];<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
     这是一个用函数返回值作为数组索引的例子。PHP 也可以用已知常量,可能之前已经见过
     <TT
CLASS="literal"
>E_*</TT
>。
     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3290"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$error_descriptions</font><font color="#007700">[</font><font color="#0000BB">E_ERROR</font><font color="#007700">]&nbsp;&nbsp;&nbsp;= </font><font color="#DD0000">"A fatal error has occured"</font><font color="#007700">;<br /></font><font color="#0000BB">$error_descriptions</font><font color="#007700">[</font><font color="#0000BB">E_WARNING</font><font color="#007700">] = </font><font color="#DD0000">"PHP issued a warning"</font><font color="#007700">;<br /></font><font color="#0000BB">$error_descriptions</font><font color="#007700">[</font><font color="#0000BB">E_NOTICE</font><font color="#007700">]&nbsp;&nbsp;= </font><font color="#DD0000">"This is just an informal notice"</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
     注意 <TT
CLASS="literal"
>E_ERROR</TT
> 也是个合法的标识符,就和第一个例子中的
     <TT
CLASS="literal"
>bar</TT
> 一样。但是上一个例子实际上和如下写法是一样的:
     <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3294"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$error_descriptions</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">] = </font><font color="#DD0000">"A fatal error has occured"</font><font color="#007700">;<br /></font><font color="#0000BB">$error_descriptions</font><font color="#007700">[</font><font color="#0000BB">2</font><font color="#007700">] = </font><font color="#DD0000">"PHP issued a warning"</font><font color="#007700">;<br /></font><font color="#0000BB">$error_descriptions</font><font color="#007700">[</font><font color="#0000BB">8</font><font color="#007700">] = </font><font color="#DD0000">"This is just an informal notice"</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
     因为 <TT
CLASS="literal"
>E_ERROR</TT
> 等于 <TT
CLASS="literal"
>1</TT
>,等等。
    </P
><P
>&#13;     如同在以上例子中解释的那样,<TT
CLASS="literal"
>$foo[bar]</TT
>
     起作用但其实是错误的。它起作用是因为根据语法的预期,<TT
CLASS="literal"
>bar</TT
> 被当成了一个常量表达式。然而,在这个例子中不存在名为
     <TT
CLASS="literal"
>bar</TT
> 的常量。PHP 就假定指的是字面上的 <TT
CLASS="literal"
>bar</TT
>,也就是字符串
     <TT
CLASS="literal"
>"bar"</TT
>,但忘记加引号了。
    </P
><DIV
CLASS="sect4"
><H4
CLASS="sect4"
><A
NAME="AEN3304"
>那么为什么这样做不好?</A
></H4
><P
>&#13;      在未来的某一时刻,PHP 开发小组可能会想新增一个常量或者关键字,或者用户可能希望以后在自己的程序中引入新的常量,那就有麻烦了。例如已经不能这样用
      <TT
CLASS="literal"
>empty</TT
> 和 <TT
CLASS="literal"
>default</TT
> 这两个词了,因为他们是<A
HREF="reserved.html"
>保留字</A
>。
     </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
       重申一次,在双引号字符串中,不给索引加上引号是合法的因此
       <TT
CLASS="literal"
>"$foo[bar]"</TT
>是合法的。至于为什么参见以上的例子和<A
HREF="language.types.string.html#language.types.string.parsing"
>字符串中的变量解析</A
>中的解释。
      </P
></BLOCKQUOTE
></DIV
></DIV
></DIV
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.array.casting"
>转换为数组</A
></H2
><P
>&#13;    对于任何的类型:整型、浮点、字符串、布尔和资源,如果将一个值转换为数组,将得到一个仅有一个元素的数组(其下标为 0),该元素即为此标量的值。
   </P
><P
>&#13;    如果将一个对象转换成一个数组,所得到的数组的元素为该对象的属性(成员变量),其键名为成员变量名。
   </P
><P
>&#13;    如果将一个 <TT
CLASS="constant"
><B
>NULL</B
></TT
> 值转换成数组,将得到一个空数组。
   </P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.array.comparing"
>比较</A
></H2
><P
>&#13;    有可能通过 <A
HREF="function.array-diff.html"
><B
CLASS="function"
>array_diff()</B
></A
> 和<A
HREF="language.operators.array.html"
>数组运算符</A
>来比较数组。
   </P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="language.types.array.examples"
>例子</A
></H2
><P
>&#13;    PHP 中的数组类型有非常多的用途,因此这里有一些例子展示数组的完整威力。
   </P
><P
>&#13;    <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3329"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// this<br /></font><font color="#0000BB">$a </font><font color="#007700">= array( </font><font color="#DD0000">'color' </font><font color="#007700">=&gt; </font><font color="#DD0000">'red'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'taste' </font><font color="#007700">=&gt; </font><font color="#DD0000">'sweet'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'shape' </font><font color="#007700">=&gt; </font><font color="#DD0000">'round'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'name'&nbsp;&nbsp;</font><font color="#007700">=&gt; </font><font color="#DD0000">'apple'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// key will be 0<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// is completely equivalent with<br /></font><font color="#0000BB">$a</font><font color="#007700">[</font><font color="#DD0000">'color'</font><font color="#007700">] = </font><font color="#DD0000">'red'</font><font color="#007700">;<br /></font><font color="#0000BB">$a</font><font color="#007700">[</font><font color="#DD0000">'taste'</font><font color="#007700">] = </font><font color="#DD0000">'sweet'</font><font color="#007700">;<br /></font><font color="#0000BB">$a</font><font color="#007700">[</font><font color="#DD0000">'shape'</font><font color="#007700">] = </font><font color="#DD0000">'round'</font><font color="#007700">;<br /></font><font color="#0000BB">$a</font><font color="#007700">[</font><font color="#DD0000">'name'</font><font color="#007700">]&nbsp;&nbsp;= </font><font color="#DD0000">'apple'</font><font color="#007700">;<br /></font><font color="#0000BB">$a</font><font color="#007700">[]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= </font><font color="#0000BB">4</font><font color="#007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// key will be 0<br /><br /></font><font color="#0000BB">$b</font><font color="#007700">[] = </font><font color="#DD0000">'a'</font><font color="#007700">;<br /></font><font color="#0000BB">$b</font><font color="#007700">[] = </font><font color="#DD0000">'b'</font><font color="#007700">;<br /></font><font color="#0000BB">$b</font><font color="#007700">[] = </font><font color="#DD0000">'c'</font><font color="#007700">;<br /></font><font color="#FF8000">// will result in the array array(0 =&gt; 'a' , 1 =&gt; 'b' , 2 =&gt; 'c'),<br />// or simply array('a', 'b', 'c')<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3331"
></A
><P
><B
>例 11-6. 使用 array()</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// Array as (property-)map<br /></font><font color="#0000BB">$map </font><font color="#007700">= array( </font><font color="#DD0000">'version'&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">=&gt; </font><font color="#0000BB">4</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'OS'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">=&gt; </font><font color="#DD0000">'Linux'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'lang'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">=&gt; </font><font color="#DD0000">'english'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'short_tags' </font><font color="#007700">=&gt; </font><font color="#0000BB">true<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// strictly numerical keys<br /></font><font color="#0000BB">$array </font><font color="#007700">= array( </font><font color="#0000BB">7</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">8</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">0</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">156</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-</font><font color="#0000BB">10<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">);<br /></font><font color="#FF8000">// this is the same as array(0 =&gt; 7, 1 =&gt; 8, ...)<br /><br /></font><font color="#0000BB">$switching </font><font color="#007700">= array(&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">10</font><font color="#007700">, </font><font color="#FF8000">// key = 0<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">5&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">=&gt;&nbsp;&nbsp;</font><font color="#0000BB">6</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">3&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">=&gt;&nbsp;&nbsp;</font><font color="#0000BB">7</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'a'&nbsp;&nbsp;</font><font color="#007700">=&gt;&nbsp;&nbsp;</font><font color="#0000BB">4</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">11</font><font color="#007700">, </font><font color="#FF8000">// key = 6 (maximum of integer-indices was 5)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'8'&nbsp;&nbsp;</font><font color="#007700">=&gt;&nbsp;&nbsp;</font><font color="#0000BB">2</font><font color="#007700">, </font><font color="#FF8000">// key = 8 (integer!)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">'02' </font><font color="#007700">=&gt; </font><font color="#0000BB">77</font><font color="#007700">, </font><font color="#FF8000">// key = '02'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">0&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">=&gt; </font><font color="#0000BB">12&nbsp;&nbsp;</font><font color="#FF8000">// the value 10 will be overwritten by 12<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">);<br /></font><font color="#FF8000">// empty array<br /></font><font color="#0000BB">$empty </font><font color="#007700">= array();<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="language.types.array.examples.loop"
></A
><P
><B
>例 11-7. 集合</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$colors </font><font color="#007700">= array(</font><font color="#DD0000">'red'</font><font color="#007700">, </font><font color="#DD0000">'blue'</font><font color="#007700">, </font><font color="#DD0000">'green'</font><font color="#007700">, </font><font color="#DD0000">'yellow'</font><font color="#007700">);<br /><br />foreach (</font><font color="#0000BB">$colors </font><font color="#007700">as </font><font color="#0000BB">$color</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"Do you like $color?</font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>上例将输出:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Do you like red?
Do you like blue?
Do you like green?
Do you like yellow?</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;    直接改变数组的值在 PHP 5 中可以通过引用传递来做到。之前的版本需要需要采取别的方法:
    <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="language.types.array.examples.changeloop"
></A
><P
><B
>例 11-8. 集合</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// PHP 5<br /></font><font color="#007700">foreach (</font><font color="#0000BB">$colors </font><font color="#007700">as &amp;</font><font color="#0000BB">$color</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$color </font><font color="#007700">= </font><font color="#0000BB">strtoupper</font><font color="#007700">(</font><font color="#0000BB">$color</font><font color="#007700">);<br />}<br />unset(</font><font color="#0000BB">$color</font><font color="#007700">); </font><font color="#FF8000">/* 确保下面对 $color 的覆盖不会影响到前一个数组单元 */<br />// 之前版本的方法<br /></font><font color="#007700">foreach (</font><font color="#0000BB">$colors </font><font color="#007700">as </font><font color="#0000BB">$key </font><font color="#007700">=&gt; </font><font color="#0000BB">$color</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$colors</font><font color="#007700">[</font><font color="#0000BB">$key</font><font color="#007700">] = </font><font color="#0000BB">strtoupper</font><font color="#007700">(</font><font color="#0000BB">$color</font><font color="#007700">);<br />}<br /><br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$colors</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>上例将输出:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Array
(
    [0] =&#62; RED
    [1] =&#62; BLUE
    [2] =&#62; GREEN
    [3] =&#62; YELLOW
)</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   </P
><P
>&#13;    本例产生一个下标从 1 开始的数组。
    <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3346"
></A
><P
><B
>例 11-9. 下标从 1 开始的数组</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$firstquarter&nbsp;&nbsp;</font><font color="#007700">= array(</font><font color="#0000BB">1 </font><font color="#007700">=&gt; </font><font color="#DD0000">'January'</font><font color="#007700">, </font><font color="#DD0000">'February'</font><font color="#007700">, </font><font color="#DD0000">'March'</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$firstquarter</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>上例将输出:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Array
(
    [1] =&#62; 'January'
    [2] =&#62; 'February'
    [3] =&#62; 'March'
)
*/
?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
  </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3351"
></A
><P
><B
>例 11-10. 填充数组</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#FF8000">// fill an array with all items from a directory<br /></font><font color="#0000BB">$handle </font><font color="#007700">= </font><font color="#0000BB">opendir</font><font color="#007700">(</font><font color="#DD0000">'.'</font><font color="#007700">);<br />while (</font><font color="#0000BB">false </font><font color="#007700">!== (</font><font color="#0000BB">$file </font><font color="#007700">= </font><font color="#0000BB">readdir</font><font color="#007700">(</font><font color="#0000BB">$handle</font><font color="#007700">))) {<br />&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">$files</font><font color="#007700">[] = </font><font color="#0000BB">$file</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">closedir</font><font color="#007700">(</font><font color="#0000BB">$handle</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;    数组是有序的。也可以使用不同的排序函数来改变顺序。更多信息参见<A
HREF="ref.array.html"
>数组函数</A
>。可以用
    <A
HREF="function.count.html"
><B
CLASS="function"
>count()</B
></A
> 函数来数出数组中元素的个数。
   </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3357"
></A
><P
><B
>例 11-11. 数组排序</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />sort</font><font color="#007700">(</font><font color="#0000BB">$files</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$files</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;    因为数组中的值可以为任意值,也可是另一个数组。这样可以产生递归或多维数组。
   </P
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN3361"
></A
><P
><B
>例 11-12. 递归和多维数组</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$fruits </font><font color="#007700">= array ( </font><font color="#DD0000">"fruits"&nbsp;&nbsp;</font><font color="#007700">=&gt; array ( </font><font color="#DD0000">"a" </font><font color="#007700">=&gt; </font><font color="#DD0000">"orange"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"b" </font><font color="#007700">=&gt; </font><font color="#DD0000">"banana"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"c" </font><font color="#007700">=&gt; </font><font color="#DD0000">"apple"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"numbers" </font><font color="#007700">=&gt; array ( </font><font color="#0000BB">1</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">2</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">3</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">4</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">5</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">6<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"holes"&nbsp;&nbsp;&nbsp;</font><font color="#007700">=&gt; array (&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"first"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">5 </font><font color="#007700">=&gt; </font><font color="#DD0000">"second"</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#DD0000">"third"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br /><br /></font><font color="#FF8000">// Some examples to address values in the array above<br /></font><font color="#007700">echo </font><font color="#0000BB">$fruits</font><font color="#007700">[</font><font color="#DD0000">"holes"</font><font color="#007700">][</font><font color="#0000BB">5</font><font color="#007700">];&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#FF8000">// prints "second"<br /></font><font color="#007700">echo </font><font color="#0000BB">$fruits</font><font color="#007700">[</font><font color="#DD0000">"fruits"</font><font color="#007700">][</font><font color="#DD0000">"a"</font><font color="#007700">]; </font><font color="#FF8000">// prints "orange"<br /></font><font color="#007700">unset(</font><font color="#0000BB">$fruits</font><font color="#007700">[</font><font color="#DD0000">"holes"</font><font color="#007700">][</font><font color="#0000BB">0</font><font color="#007700">]);&nbsp;&nbsp;</font><font color="#FF8000">// remove "first"<br /><br />// Create a new multi-dimensional array<br /></font><font color="#0000BB">$juices</font><font color="#007700">[</font><font color="#DD0000">"apple"</font><font color="#007700">][</font><font color="#DD0000">"green"</font><font color="#007700">] = </font><font color="#DD0000">"good"</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
><P
>&#13;    需要注意数组的赋值总是会涉及到值的拷贝。这还意味着在 <A
HREF="function.current.html"
><B
CLASS="function"
>current()</B
></A
>
    以及类似函数中使用的内部数组指针会被重置。需要在复制数组时用引用符号(&#38;)。
    <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN3366"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />$arr1 </font><font color="#007700">= array(</font><font color="#0000BB">2</font><font color="#007700">, </font><font color="#0000BB">3</font><font color="#007700">);<br /></font><font color="#0000BB">$arr2 </font><font color="#007700">= </font><font color="#0000BB">$arr1</font><font color="#007700">;<br /></font><font color="#0000BB">$arr2</font><font color="#007700">[] = </font><font color="#0000BB">4</font><font color="#007700">; </font><font color="#FF8000">// $arr2 is changed,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// $arr1 is still array(2,3)<br /><br /></font><font color="#0000BB">$arr3 </font><font color="#007700">= &amp;</font><font color="#0000BB">$arr1</font><font color="#007700">;<br /></font><font color="#0000BB">$arr3</font><font color="#007700">[] = </font><font color="#0000BB">4</font><font color="#007700">; </font><font color="#FF8000">// now $arr1 and $arr3 are the same<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="language.types.string.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>起始页</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.types.object.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>字符串</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.types.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>对象</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>