Sophie

Sophie

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

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
>array_map</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="PHP 手册"
HREF="index.html"><LINK
REL="UP"
TITLE="Array 数组函数"
HREF="ref.array.html"><LINK
REL="PREVIOUS"
TITLE="array_keys"
HREF="function.array-keys.html"><LINK
REL="NEXT"
TITLE="array_merge_recursive"
HREF="function.array-merge-recursive.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=UTF-8"></HEAD
><BODY
CLASS="refentry"
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="function.array-keys.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.array-merge-recursive.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.array-map"
></A
>array_map</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN11355"
></A
><P
>    (PHP 4 &#62;= 4.0.6, PHP 5)</P
>array_map&nbsp;--&nbsp;
     将回调函数作用到给定数组的单元上
    </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN11358"
></A
><H2
>说明</H2
>array <B
CLASS="methodname"
>array_map</B
> ( callback callback, array arr1 [, array ...] )<BR
></BR
><P
>&#13;     <B
CLASS="function"
>array_map()</B
> 返回一个数组,该数组包含了
     <CODE
CLASS="parameter"
>arr1</CODE
> 中的所有单元经过
     <CODE
CLASS="parameter"
>callback</CODE
> 作用过之后的单元。<CODE
CLASS="parameter"
>callback</CODE
>
     接受的参数数目应该和传递给
     <B
CLASS="function"
>array_map()</B
> 函数的数组数目一致。
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN11379"
></A
><P
><B
>例 1. <B
CLASS="function"
>array_map()</B
> 例子</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">function </font><font color="#0000BB">cube</font><font color="#007700">(</font><font color="#0000BB">$n</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return(</font><font color="#0000BB">$n </font><font color="#007700">* </font><font color="#0000BB">$n </font><font color="#007700">* </font><font color="#0000BB">$n</font><font color="#007700">);<br />}<br /><br /></font><font color="#0000BB">$a </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">$b </font><font color="#007700">= </font><font color="#0000BB">array_map</font><font color="#007700">(</font><font color="#DD0000">"cube"</font><font color="#007700">, </font><font color="#0000BB">$a</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$b</font><font color="#007700">);<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
>&#13;       这使得 <CODE
CLASS="varname"
>$b</CODE
> 成为:
      </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="screen"
>Array
(
    [0] =&#62; 1
    [1] =&#62; 8
    [2] =&#62; 27
    [3] =&#62; 64
    [4] =&#62; 125
)</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN11387"
></A
><P
><B
>例 2. <B
CLASS="function"
>array_map()</B
> - 使用更多的数组</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">function </font><font color="#0000BB">show_Spanish</font><font color="#007700">(</font><font color="#0000BB">$n</font><font color="#007700">, </font><font color="#0000BB">$m</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return(</font><font color="#DD0000">"The number $n is called $m in Spanish"</font><font color="#007700">);<br />}<br /><br />function </font><font color="#0000BB">map_Spanish</font><font color="#007700">(</font><font color="#0000BB">$n</font><font color="#007700">, </font><font color="#0000BB">$m</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return(array(</font><font color="#0000BB">$n </font><font color="#007700">=&gt; </font><font color="#0000BB">$m</font><font color="#007700">));<br />}<br /><br /></font><font color="#0000BB">$a </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">$b </font><font color="#007700">= array(</font><font color="#DD0000">"uno"</font><font color="#007700">, </font><font color="#DD0000">"dos"</font><font color="#007700">, </font><font color="#DD0000">"tres"</font><font color="#007700">, </font><font color="#DD0000">"cuatro"</font><font color="#007700">, </font><font color="#DD0000">"cinco"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$c </font><font color="#007700">= </font><font color="#0000BB">array_map</font><font color="#007700">(</font><font color="#DD0000">"show_Spanish"</font><font color="#007700">, </font><font color="#0000BB">$a</font><font color="#007700">, </font><font color="#0000BB">$b</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$c</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$d </font><font color="#007700">= </font><font color="#0000BB">array_map</font><font color="#007700">(</font><font color="#DD0000">"map_Spanish"</font><font color="#007700">, </font><font color="#0000BB">$a </font><font color="#007700">, </font><font color="#0000BB">$b</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$d</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"
>// printout of $c
Array
(
    [0] =&#62; The number 1 is called uno in Spanish
    [1] =&#62; The number 2 is called dos in Spanish
    [2] =&#62; The number 3 is called tres in Spanish
    [3] =&#62; The number 4 is called cuatro in Spanish
    [4] =&#62; The number 5 is called cinco in Spanish
)

// printout of $d
Array
(
    [0] =&#62; Array
        (
            [1] =&#62; uno
        )

    [1] =&#62; Array
        (
            [2] =&#62; dos
        )

    [2] =&#62; Array
        (
            [3] =&#62; tres
        )

    [3] =&#62; Array
        (
            [4] =&#62; cuatro
        )

    [4] =&#62; Array
        (
            [5] =&#62; cinco
        )

)</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     通常使用了两个或更多数组时,它们的长度应该相同,因为回调函数是平行作用于相应的单元上的。如果数组的长度不同,则最短的一个将被用空的单元扩充。
    </P
><P
>&#13;     本函数一个有趣的用法是构造一个数组的数组,这可以很容易的通过用 <TT
CLASS="constant"
><B
>NULL</B
></TT
> 作为回调函数名来实现。
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN11397"
></A
><P
><B
>例 3. 建立一个数组的数组</B
></P
><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">, </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">$b </font><font color="#007700">= array(</font><font color="#DD0000">"one"</font><font color="#007700">, </font><font color="#DD0000">"two"</font><font color="#007700">, </font><font color="#DD0000">"three"</font><font color="#007700">, </font><font color="#DD0000">"four"</font><font color="#007700">, </font><font color="#DD0000">"five"</font><font color="#007700">);<br /></font><font color="#0000BB">$c </font><font color="#007700">= array(</font><font color="#DD0000">"uno"</font><font color="#007700">, </font><font color="#DD0000">"dos"</font><font color="#007700">, </font><font color="#DD0000">"tres"</font><font color="#007700">, </font><font color="#DD0000">"cuatro"</font><font color="#007700">, </font><font color="#DD0000">"cinco"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$d </font><font color="#007700">= </font><font color="#0000BB">array_map</font><font color="#007700">(</font><font color="#0000BB">null</font><font color="#007700">, </font><font color="#0000BB">$a</font><font color="#007700">, </font><font color="#0000BB">$b</font><font color="#007700">, </font><font color="#0000BB">$c</font><font color="#007700">);<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">$d</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; Array
        (
            [0] =&#62; 1
            [1] =&#62; one
            [2] =&#62; uno
        )

    [1] =&#62; Array
        (
            [0] =&#62; 2
            [1] =&#62; two
            [2] =&#62; dos
        )

    [2] =&#62; Array
        (
            [0] =&#62; 3
            [1] =&#62; three
            [2] =&#62; tres
        )

    [3] =&#62; Array
        (
            [0] =&#62; 4
            [1] =&#62; four
            [2] =&#62; cuatro
        )

    [4] =&#62; Array
        (
            [0] =&#62; 5
            [1] =&#62; five
            [2] =&#62; cinco
        )

)</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     参见 <A
HREF="function.array-filter.html"
><B
CLASS="function"
>array_filter()</B
></A
>,<A
HREF="function.array-reduce.html"
><B
CLASS="function"
>array_reduce()</B
></A
>,<A
HREF="function.array-walk.html"
><B
CLASS="function"
>array_walk()</B
></A
>
     和有关 <A
HREF="language.pseudo-types.html#language.types.callback"
>callback</A
> 类型的信息。
    </P
></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="function.array-keys.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="function.array-merge-recursive.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>array_keys</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ref.array.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>array_merge_recursive</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>