<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >in_array</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="extract" HREF="function.extract.html"><LINK REL="NEXT" TITLE="key" HREF="function.key.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.extract.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.key.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.in-array" ></A >in_array</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN13039" ></A ><P > (PHP 4, PHP 5)</P >in_array -- 检查数组中是否存在某个值</DIV ><DIV CLASS="refsect1" ><A NAME="AEN13042" ></A ><H2 >说明</H2 >bool <B CLASS="methodname" >in_array</B > ( mixed needle, array haystack [, bool strict] )<BR ></BR ><P > 在 <CODE CLASS="parameter" >haystack</CODE > 中搜索 <CODE CLASS="parameter" >needle</CODE >,如果找到则返回 <TT CLASS="constant" ><B >TRUE</B ></TT >,否则返回 <TT CLASS="constant" ><B >FALSE</B ></TT >。 </P ><P > 如果第三个参数 <CODE CLASS="parameter" >strict</CODE > 的值为 <TT CLASS="constant" ><B >TRUE</B ></TT > 则 <B CLASS="function" >in_array()</B > 函数还会检查 <CODE CLASS="parameter" >needle</CODE > 的<A HREF="language.types.html" >类型</A >是否和 <CODE CLASS="parameter" >haystack</CODE > 中的相同。 </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > 如果 <CODE CLASS="parameter" >needle</CODE > 是字符串,则比较是区分大小写的。 </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > 在 PHP 版本 4.2.0 之前,<CODE CLASS="parameter" >needle</CODE > 不允许是一个数组。 </P ></BLOCKQUOTE ></DIV ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN13075" ></A ><P ><B >例 1. <B CLASS="function" >in_array()</B > 例子</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$os </font><font color="#007700">= array(</font><font color="#DD0000">"Mac"</font><font color="#007700">, </font><font color="#DD0000">"NT"</font><font color="#007700">, </font><font color="#DD0000">"Irix"</font><font color="#007700">, </font><font color="#DD0000">"Linux"</font><font color="#007700">);<br />if (</font><font color="#0000BB">in_array</font><font color="#007700">(</font><font color="#DD0000">"Irix"</font><font color="#007700">, </font><font color="#0000BB">$os</font><font color="#007700">)) {<br /> echo </font><font color="#DD0000">"Got Irix"</font><font color="#007700">;<br />}<br />if (</font><font color="#0000BB">in_array</font><font color="#007700">(</font><font color="#DD0000">"mac"</font><font color="#007700">, </font><font color="#0000BB">$os</font><font color="#007700">)) {<br /> echo </font><font color="#DD0000">"Got mac"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > 第二个条件失败,因为 <B CLASS="function" >in_array()</B > 是区分大小写的,所以以上程序显示为: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> Got Irix</font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN13083" ></A ><P ><B >例 2. <B CLASS="function" >in_array()</B > 严格类型检查例子</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$a </font><font color="#007700">= array(</font><font color="#DD0000">'1.10'</font><font color="#007700">, </font><font color="#0000BB">12.4</font><font color="#007700">, </font><font color="#0000BB">1.13</font><font color="#007700">);<br /><br />if (</font><font color="#0000BB">in_array</font><font color="#007700">(</font><font color="#DD0000">'12.4'</font><font color="#007700">, </font><font color="#0000BB">$a</font><font color="#007700">, </font><font color="#0000BB">true</font><font color="#007700">)) {<br /> echo </font><font color="#DD0000">"'12.4' found with strict check\n"</font><font color="#007700">;<br />}<br />if (</font><font color="#0000BB">in_array</font><font color="#007700">(</font><font color="#0000BB">1.13</font><font color="#007700">, </font><font color="#0000BB">$a</font><font color="#007700">, </font><font color="#0000BB">true</font><font color="#007700">)) {<br /> echo </font><font color="#DD0000">"1.13 found with strict check\n"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >上例将输出:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> 1.13 found with strict check</font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN13090" ></A ><P ><B >例 3. <B CLASS="function" >in_array()</B > 中用数组作为 needle</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$a </font><font color="#007700">= array(array(</font><font color="#DD0000">'p'</font><font color="#007700">, </font><font color="#DD0000">'h'</font><font color="#007700">), array(</font><font color="#DD0000">'p'</font><font color="#007700">, </font><font color="#DD0000">'r'</font><font color="#007700">), </font><font color="#DD0000">'o'</font><font color="#007700">);<br /><br />if (</font><font color="#0000BB">in_array</font><font color="#007700">(array(</font><font color="#DD0000">'p'</font><font color="#007700">, </font><font color="#DD0000">'h'</font><font color="#007700">), </font><font color="#0000BB">$a</font><font color="#007700">)) {<br /> echo </font><font color="#DD0000">"'ph' was found\n"</font><font color="#007700">;<br />}<br />if (</font><font color="#0000BB">in_array</font><font color="#007700">(array(</font><font color="#DD0000">'f'</font><font color="#007700">, </font><font color="#DD0000">'i'</font><font color="#007700">), </font><font color="#0000BB">$a</font><font color="#007700">)) {<br /> echo </font><font color="#DD0000">"'fi' was found\n"</font><font color="#007700">;<br />}<br />if (</font><font color="#0000BB">in_array</font><font color="#007700">(</font><font color="#DD0000">'o'</font><font color="#007700">, </font><font color="#0000BB">$a</font><font color="#007700">)) {<br /> echo </font><font color="#DD0000">"'o' was found\n"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >上例将输出:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >'ph' was found 'o' was found</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 参见 <A HREF="function.array-search.html" ><B CLASS="function" >array_search()</B ></A >,<A HREF="function.array-key-exists.html" ><B CLASS="function" >array_key_exists()</B ></A > 和 <A HREF="function.isset.html" ><B CLASS="function" >isset()</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.extract.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.key.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >extract</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.array.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >key</TD ></TR ></TABLE ></DIV ></BODY ></HTML >