Sophie

Sophie

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

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_filter</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_fill"
HREF="function.array-fill.html"><LINK
REL="NEXT"
TITLE="array_flip"
HREF="function.array-flip.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-fill.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.array-flip.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.array-filter"
></A
>array_filter</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN10961"
></A
><P
>    (PHP 4 &#62;= 4.0.6, PHP 5)</P
>array_filter&nbsp;--&nbsp;
     用回调函数过滤数组中的单元
    </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN10964"
></A
><H2
>说明</H2
>array <B
CLASS="methodname"
>array_filter</B
> ( array input [, callback callback] )<BR
></BR
><P
>&#13;     <B
CLASS="function"
>array_filter()</B
> 依次将
     <CODE
CLASS="parameter"
>input</CODE
> 数组中的每个值传递到
     <CODE
CLASS="parameter"
>callback</CODE
> 函数。如果
     <CODE
CLASS="parameter"
>callback</CODE
> 函数返回 <TT
CLASS="constant"
><B
>TRUE</B
></TT
>,则
     <CODE
CLASS="parameter"
>input</CODE
>
     数组的当前值会被包含在返回的结果数组中。数组的键名保留不变。
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN10983"
></A
><P
><B
>例 1. <B
CLASS="function"
>array_filter()</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">odd</font><font color="#007700">(</font><font color="#0000BB">$var</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return(</font><font color="#0000BB">$var </font><font color="#007700">% </font><font color="#0000BB">2 </font><font color="#007700">== </font><font color="#0000BB">1</font><font color="#007700">);<br />}<br /><br />function </font><font color="#0000BB">even</font><font color="#007700">(</font><font color="#0000BB">$var</font><font color="#007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return(</font><font color="#0000BB">$var </font><font color="#007700">% </font><font color="#0000BB">2 </font><font color="#007700">== </font><font color="#0000BB">0</font><font color="#007700">);<br />}<br /><br /></font><font color="#0000BB">$array1 </font><font color="#007700">= array(</font><font color="#DD0000">"a"</font><font color="#007700">=&gt;</font><font color="#0000BB">1</font><font color="#007700">, </font><font color="#DD0000">"b"</font><font color="#007700">=&gt;</font><font color="#0000BB">2</font><font color="#007700">, </font><font color="#DD0000">"c"</font><font color="#007700">=&gt;</font><font color="#0000BB">3</font><font color="#007700">, </font><font color="#DD0000">"d"</font><font color="#007700">=&gt;</font><font color="#0000BB">4</font><font color="#007700">, </font><font color="#DD0000">"e"</font><font color="#007700">=&gt;</font><font color="#0000BB">5</font><font color="#007700">);<br /></font><font color="#0000BB">$array2 </font><font color="#007700">= array(</font><font color="#0000BB">6</font><font color="#007700">, </font><font color="#0000BB">7</font><font color="#007700">, </font><font color="#0000BB">8</font><font color="#007700">, </font><font color="#0000BB">9</font><font color="#007700">, </font><font color="#0000BB">10</font><font color="#007700">, </font><font color="#0000BB">11</font><font color="#007700">, </font><font color="#0000BB">12</font><font color="#007700">);<br /><br />echo </font><font color="#DD0000">"Odd :\n"</font><font color="#007700">;<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">array_filter</font><font color="#007700">(</font><font color="#0000BB">$array1</font><font color="#007700">, </font><font color="#DD0000">"odd"</font><font color="#007700">));<br />echo </font><font color="#DD0000">"Even:\n"</font><font color="#007700">;<br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">array_filter</font><font color="#007700">(</font><font color="#0000BB">$array2</font><font color="#007700">, </font><font color="#DD0000">"even"</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
><code><font color="#000000">
Odd :<br />Array<br />(<br />&nbsp;&nbsp;&nbsp;&nbsp;[a] =&gt; 1<br />&nbsp;&nbsp;&nbsp;&nbsp;[c] =&gt; 3<br />&nbsp;&nbsp;&nbsp;&nbsp;[e] =&gt; 5<br />)<br />Even:<br />Array<br />(<br />&nbsp;&nbsp;&nbsp;&nbsp;[0] =&gt; 6<br />&nbsp;&nbsp;&nbsp;&nbsp;[2] =&gt; 8<br />&nbsp;&nbsp;&nbsp;&nbsp;[4] =&gt; 10<br />&nbsp;&nbsp;&nbsp;&nbsp;[6] =&gt; 12<br />)</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     用户不应在回调函数中修改数组本身。例如增加/删除单元或者对
     <B
CLASS="function"
>array_filter()</B
> 正在作用的数组进行
     unset。如果数组改变了,此函数的行为没有定义。
    </P
><P
>&#13;     如果没有提供 <CODE
CLASS="parameter"
>callback</CODE
>
     函数,<B
CLASS="function"
>array_filter()</B
>
     将删除 <CODE
CLASS="parameter"
>input</CODE
> 中所有等值为
     <TT
CLASS="constant"
><B
>FALSE</B
></TT
> 的条目。更多信息见<A
HREF="language.types.boolean.html#language.types.boolean.casting"
>转换为布尔值</A
>。
    </P
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN10998"
></A
><P
><B
>例 2. <B
CLASS="function"
>array_filter()</B
> 无
      <CODE
CLASS="parameter"
>callback</CODE
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /><br />$entry </font><font color="#007700">= array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">0 </font><font color="#007700">=&gt; </font><font color="#DD0000">'foo'</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">1 </font><font color="#007700">=&gt; </font><font color="#0000BB">false</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">2 </font><font color="#007700">=&gt; -</font><font color="#0000BB">1</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">3 </font><font color="#007700">=&gt; </font><font color="#0000BB">null</font><font color="#007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#0000BB">4 </font><font color="#007700">=&gt; </font><font color="#DD0000">''<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#007700">);<br /><br /></font><font color="#0000BB">print_r</font><font color="#007700">(</font><font color="#0000BB">array_filter</font><font color="#007700">(</font><font color="#0000BB">$entry</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; foo
    [2] =&#62; -1
)</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     参见 <A
HREF="function.array-map.html"
><B
CLASS="function"
>array_map()</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
>。
    </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-fill.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-flip.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>array_fill</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_flip</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>