uTools-Manuals/docs/php/count.html
2019-04-08 23:22:26 +08:00

172 lines
10 KiB
HTML
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.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>计算数组中的单元数目,或对象中的属性个数</title>
</head>
<body class="docs"><div id="layout">
<div id="layout-content"><div id="function.count" class="refentry">
<div class="refnamediv">
<h1 class="refname">count</h1>
<p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">count</span> &mdash; <span class="dc-title">计算数组中的单元数目,或对象中的属性个数</span></p>
</div>
<div class="refsect1 description" id="refsect1-function.count-description">
<h3 class="title">说明</h3>
<div class="methodsynopsis dc-description">
<span class="methodname"><strong>count</strong></span>
( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$array_or_countable</code></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$mode</code><span class="initializer"> = COUNT_NORMAL</span></span>
] ) : <span class="type">int</span></div>
<p class="para rdfs-comment">
统计出数组里的所有元素的数量,或者对象里的东西。
</p>
<p class="para">
对于对象,如果安装了 <a href="ref.spl.html" class="link">SPL</a>,可以通过实现
<em>Countable</em> 接口对 <span class="function"><strong>count()</strong></span>挂钩hook
。该接口只有一个方法
<span class="methodname"><a href="countable.count.html" class="methodname">Countable::count()</a></span>,此方法为 <span class="function"><strong>count()</strong></span>
函数返回值。
</p>
<p class="para">
关于 PHP 中如何实现和使用数组可以参考手册中<a href="language.types.array.html" class="link">数组</a>章节中的详细描述。
</p>
</div>
<div class="refsect1 parameters" id="refsect1-function.count-parameters">
<h3 class="title">参数</h3>
<p class="para">
<dl>
<dt>
<code class="parameter">array_or_countable</code></dt>
<dd>
<p class="para">
数组或者 <a href="class.countable.html" class="classname">Countable</a> 对象。
</p>
</dd>
<dt>
<code class="parameter">mode</code></dt>
<dd>
<p class="para">
如果可选的 <code class="parameter">mode</code> 参数设为
<strong><code>COUNT_RECURSIVE</code></strong>(或 1<span class="function"><strong>count()</strong></span>
将递归地对数组计数。对计算多维数组的所有单元尤其有用。
</p>
<div class="caution"><strong class="caution">Caution</strong>
<p class="para">
<span class="function"><strong>count()</strong></span> 能检测递归来避免无限循环,但每次出现时会产生 <strong><code>E_WARNING</code></strong> 错误
(如果 array 不止一次包含了自身)并返回大于预期的统计数字。
</p>
</div>
</dd>
</dl>
</p>
</div>
<div class="refsect1 returnvalues" id="refsect1-function.count-returnvalues">
<h3 class="title">返回值</h3>
<p class="para">
返回 <code class="parameter">array_or_countable</code> 中的单元数目。
如果参数既不是数组,也不是实现
<em>Countable</em> 接口的对象,将返回
<em>1</em>
有个例外:如果
<code class="parameter">array_or_countable</code><strong><code>NULL</code></strong> 则结果是 <em>0</em>
</p>
</div>
<div class="refsect1 examples" id="refsect1-function.count-examples">
<h3 class="title">范例</h3>
<p class="para">
<div class="example" id="example-6090">
<p><strong>Example #1 <span class="function"><strong>count()</strong></span> 例子</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]&nbsp;&nbsp;=&nbsp;</span><span style="color: #0000BB">7</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">[</span><span style="color: #0000BB">5</span><span style="color: #007700">]&nbsp;&nbsp;=&nbsp;</span><span style="color: #0000BB">9</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">[</span><span style="color: #0000BB">10</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">11</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">false</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
<div class="example-contents"><p>以上例程会输出:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
int(3)
int(3)
Warning: count(): Parameter must be an array or an object that implements Countable in … on line 12 // PHP 7.2 起
int(0)
Warning: count(): Parameter must be an array or an object that implements Countable in … on line 14 // PHP 7.2 起
int(1)
</pre></div>
</div>
</div>
</p>
<p class="para">
<div class="example" id="example-6091">
<p><strong>Example #2 递归 <span class="function"><strong>count()</strong></span> 例子</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$food&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'fruits'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'orange'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'banana'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'veggie'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'carrot'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'collard'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'pea'</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;recursive&nbsp;count<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$food</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">COUNT_RECURSIVE</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;output&nbsp;8<br /><br />//&nbsp;normal&nbsp;count<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$food</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;output&nbsp;2<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
</div>
</p>
</div>
<div class="refsect1 changelog" id="refsect1-function.count-changelog">
<h3 class="title">更新日志</h3>
<table class="doctable informaltable">
<thead>
<tr>
<th>版本</th>
<th>说明</th>
</tr>
</thead>
<tbody class="tbody">
<tr>
<td>7.2.0</td>
<td>
当无效的 countable 类型传递给 <code class="parameter">array_or_countable</code> 参数时,<span class="function"><strong>count()</strong></span> 会产生警告。
</td>
</tr>
</tbody>
</table>
</div>
<div class="refsect1 seealso" id="refsect1-function.count-seealso">
<h3 class="title">参见</h3>
<p class="para">
<ul class="simplelist">
<li class="member"><span class="function"><a href="is_array.html" class="function" rel="rdfs-seeAlso">is_array()</a> - 检测变量是否是数组</span></li>
<li class="member"><span class="function"><a href="isset.html" class="function" rel="rdfs-seeAlso">isset()</a> - 检测变量是否已设置并且非 NULL</span></li>
<li class="member"><span class="function"><a href="empty.html" class="function" rel="rdfs-seeAlso">empty()</a> - 检查一个变量是否为空</span></li>
<li class="member"><span class="function"><a href="strlen.html" class="function" rel="rdfs-seeAlso">strlen()</a> - 获取字符串长度</span></li>
</ul>
</p>
</div>
</div></div></div></body></html>