Sophie

Sophie

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

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_rand</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_push"
HREF="function.array-push.html"><LINK
REL="NEXT"
TITLE="array_reduce"
HREF="function.array-reduce.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-push.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.array-reduce.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="function.array-rand"
></A
>array_rand</H1
><DIV
CLASS="refnamediv"
><A
NAME="AEN11738"
></A
><P
>    (PHP 4, PHP 5)</P
>array_rand&nbsp;--&nbsp;
     从数组中随机取出一个或多个单元
    </DIV
><DIV
CLASS="refsect1"
><A
NAME="AEN11741"
></A
><H2
>说明</H2
>mixed <B
CLASS="methodname"
>array_rand</B
> ( array input [, int num_req] )<BR
></BR
><P
>&#13;     <B
CLASS="function"
>array_rand()</B
> 在你想从数组中取出一个或多个随机的单元时相当有用。它接受
     <CODE
CLASS="parameter"
>input</CODE
> 作为输入数组和一个可选的参数
     <CODE
CLASS="parameter"
>num_req</CODE
>,指明了你想取出多少个单元 - 如果没有指定,默认为 1。
    </P
><P
>&#13;     如果你只取出一个,<B
CLASS="function"
>array_rand()</B
>
     返回一个随机单元的键名,否则就返回一个包含随机键名的数组。这样你就可以随机从数组中取出键名和值。
    </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>自 PHP 4.2.0 起,不再需要用
<A
HREF="function.srand.html"
><B
CLASS="function"
>srand()</B
></A
> 或 <A
HREF="function.mt-srand.html"
><B
CLASS="function"
>mt_srand()</B
></A
>
函数给随机数发生器播种,现已自动完成。</P
></BLOCKQUOTE
></DIV
><P
>&#13;     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN11763"
></A
><P
><B
>例 1. <B
CLASS="function"
>array_rand()</B
> 例子</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br />srand</font><font color="#007700">((float) </font><font color="#0000BB">microtime</font><font color="#007700">() * </font><font color="#0000BB">10000000</font><font color="#007700">);<br /></font><font color="#0000BB">$input </font><font color="#007700">= array(</font><font color="#DD0000">"Neo"</font><font color="#007700">, </font><font color="#DD0000">"Morpheus"</font><font color="#007700">, </font><font color="#DD0000">"Trinity"</font><font color="#007700">, </font><font color="#DD0000">"Cypher"</font><font color="#007700">, </font><font color="#DD0000">"Tank"</font><font color="#007700">);<br /></font><font color="#0000BB">$rand_keys </font><font color="#007700">= </font><font color="#0000BB">array_rand</font><font color="#007700">(</font><font color="#0000BB">$input</font><font color="#007700">, </font><font color="#0000BB">2</font><font color="#007700">);<br />print </font><font color="#0000BB">$input</font><font color="#007700">[</font><font color="#0000BB">$rand_keys</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">]] . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br />print </font><font color="#0000BB">$input</font><font color="#007700">[</font><font color="#0000BB">$rand_keys</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">]] . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><P
>&#13;     参见 <A
HREF="function.shuffle.html"
><B
CLASS="function"
>shuffle()</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-push.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-reduce.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>array_push</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_reduce</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>