1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-26 08:02:16 +01:00
Files
archived-doc-zh/reference/array/functions/count.xml
Dai Jie 264ee58391 Sync array functions .
git-svn-id: https://svn.php.net/repository/phpdoc/zh/trunk@327980 c90b9560-bf6c-de11-be94-00142212c4b1
2012-10-10 12:54:58 +00:00

180 lines
4.8 KiB
XML
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- $Author$ -->
<!-- EN-Revision: 9190379067d4cfae59c5234321b6bbdb71214784 Maintainer: dallas Status: ready -->
<refentry xml:id="function.count" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>count</refname>
<refpurpose>计算数组中的单元数目或对象中的属性个数</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>int</type><methodname>count</methodname>
<methodparam><type>mixed</type><parameter>var</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>mode</parameter><initializer>COUNT_NORMAL</initializer></methodparam>
</methodsynopsis>
<para>
统计一个数组里的所有元素,或者一个对象里的东西。
</para>
<para>
对于对象,如果安装了 <link linkend="ref.spl">SPL</link>,可以通过实现
<literal>Countable</literal> 接口来调用
<function>count</function>。该接口只有一个方法
<methodname>Countable::count</methodname>,此方法返回 <function>count</function>
函数的返回值。
</para>
<para>
关于 PHP 中如何实现和使用数组可以参考手册中<link linkend="language.types.array">数组</link>章节中的详细描述。
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>var</parameter></term>
<listitem>
<para>
数组或者对象。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>mode</parameter></term>
<listitem>
<para>
如果可选的 <parameter>mode</parameter> 参数设为
<constant>COUNT_RECURSIVE</constant>(或 1<function>count</function>
将递归地对数组计数。对计算多维数组的所有单元尤其有用。<parameter>mode</parameter>
的默认值是 <literal>0</literal><function>count</function>
识别不了无限递归。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
返回 <parameter>var</parameter> 中的单元数目。
如果 <parameter>var</parameter> 不是数组类型或者实现了
<literal>Countable</literal> 接口的对象,将返回
<literal>1</literal>,有一个例外,如果
<parameter>var</parameter>&null; 则结果是 <literal>0</literal>
</para>
<caution>
<para>
<function>count</function> 对没有初始化的变量返回 0但对于空的数组也会返回 0。用
<function>isset</function> 来测试变量是否已经初始化。
</para>
</caution>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>4.2.0</entry>
<entry>
添加了可选的 <parameter>mode</parameter> 参数。
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>count</function> 例子</title>
<programlisting role="php">
<![CDATA[
<?php
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);
// $result == 3
$b[0] = 7;
$b[5] = 9;
$b[10] = 11;
$result = count($b);
// $result == 3
$result = count(null);
// $result == 0
$result = count(false);
// $result == 1
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>递归 <function>count</function> 例子</title>
<programlisting role="php">
<![CDATA[
<?php
$food = array('fruits' => array('orange', 'banana', 'apple'),
'veggie' => array('carrot', 'collard', 'pea'));
// recursive count
echo count($food, COUNT_RECURSIVE); // output 8
// normal count
echo count($food); // output 2
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>is_array</function></member>
<member><function>isset</function></member>
<member><function>strlen</function></member>
</simplelist>
</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:"~/.phpdoc/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
-->