mirror of
https://github.com/php/doc-zh.git
synced 2026-03-26 08:02:16 +01:00
git-svn-id: https://svn.php.net/repository/phpdoc/zh/trunk@95662 c90b9560-bf6c-de11-be94-00142212c4b1
138 lines
3.4 KiB
XML
Executable File
138 lines
3.4 KiB
XML
Executable File
<?xml version="1.0" encoding="gb2312"?>
|
|
<!-- $Revision: 1.1 $ -->
|
|
<!-- $Author: dallas $ -->
|
|
<!-- EN-Revision: 1.5 Maintainer: dallas Status: ready -->
|
|
<refentry id="function.in-array">
|
|
<refnamediv>
|
|
<refname>in_array</refname>
|
|
<refpurpose>如果数组中存在该值则返回 &true;</refpurpose>
|
|
</refnamediv>
|
|
<refsect1>
|
|
<title>说明</title>
|
|
<methodsynopsis>
|
|
<type>bool</type><methodname>in_array</methodname>
|
|
<methodparam><type>mixed</type><parameter>needle</parameter></methodparam>
|
|
<methodparam><type>array</type><parameter>haystack</parameter></methodparam>
|
|
<methodparam choice="opt"><type>bool</type><parameter>strict</parameter></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
在 <parameter>haystack</parameter> 中搜索
|
|
<parameter>needle</parameter>,如果找到则返回 &true;,否则返回 &false;。
|
|
</para>
|
|
<para>
|
|
如果第三个参数 <parameter>strict</parameter> 的值为
|
|
&true; 则 <function>in_array</function> 函数还会检查
|
|
<parameter>needle</parameter> 的<link linkend="language.types">类型</link>是否和
|
|
<parameter>haystack</parameter> 中的相同。
|
|
</para>
|
|
<note>
|
|
<para>
|
|
如果 <parameter>needle</parameter> 是字符串,则比较是区分大小写的。
|
|
</para>
|
|
</note>
|
|
<note>
|
|
<para>
|
|
在 PHP 版本 4.2.0 之前,<parameter>needle</parameter> 不允许是一个数组。
|
|
</para>
|
|
</note>
|
|
<para>
|
|
<example>
|
|
<title><function>in_array</function> 例子</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
$os = array ("Mac", "NT", "Irix", "Linux");
|
|
if (in_array ("Irix", $os)) {
|
|
print "Got Irix";
|
|
}
|
|
if (in_array ("mac", $os)) {
|
|
print "Got mac";
|
|
}
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
第二个条件失败,因为 <function>in_array</function>
|
|
是区分大小写的,所以以上程序显示为:
|
|
<screen role="php">
|
|
<![CDATA[
|
|
Got Irix
|
|
]]>
|
|
</screen>
|
|
</para>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title><function>in_array</function> 严格类型检查例子</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$a = array('1.10', 12.4, 1.13);
|
|
|
|
if (in_array('12.4', $a, TRUE))
|
|
echo "'12.4' found with strict check\n";
|
|
if (in_array(1.13, $a, TRUE))
|
|
echo "1.13 found with strict check\n";
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
将显示:
|
|
<screen role="php">
|
|
<![CDATA[
|
|
1.13 found with strict check
|
|
]]>
|
|
</screen>
|
|
</para>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title><function>in_array</function> 中用数组作为 needle</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$a = array(array('p', 'h'), array('p', 'r'), 'o');
|
|
|
|
if (in_array(array ('p', 'h'), $a))
|
|
echo "'ph' was found\n";
|
|
if (in_array(array ('f', 'i'), $a))
|
|
echo "'fi' was found\n";
|
|
if (in_array('o', $a))
|
|
echo "'o' was found\n";
|
|
?>
|
|
|
|
// This will output:
|
|
|
|
'ph' was found
|
|
'o' was found
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
参见 <function>array_search</function>。
|
|
</para>
|
|
</refsect1>
|
|
</refentry>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"../../../../manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|