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@95699 c90b9560-bf6c-de11-be94-00142212c4b1
82 lines
2.3 KiB
XML
Executable File
82 lines
2.3 KiB
XML
Executable File
<?xml version="1.0" encoding="gb2312"?>
|
||
<!-- $Revision: 1.2 $ -->
|
||
<!-- $Author: dallas $ -->
|
||
<!-- EN-Revision: 1.4 Maintainer: dallas Status: ready -->
|
||
<refentry id="function.array-reduce">
|
||
<refnamediv>
|
||
<refname>array_reduce</refname>
|
||
<refpurpose>
|
||
用回调函数迭代地将数组简化为单一的值
|
||
</refpurpose>
|
||
</refnamediv>
|
||
<refsect1>
|
||
<title>说明</title>
|
||
<methodsynopsis>
|
||
<type>mixed</type><methodname>array_reduce</methodname>
|
||
<methodparam><type>array</type><parameter>input</parameter></methodparam>
|
||
<methodparam><type>mixed</type><parameter>callback</parameter></methodparam>
|
||
<methodparam choice="opt"><type>int</type><parameter>initial</parameter></methodparam>
|
||
</methodsynopsis>
|
||
<para>
|
||
<function>array_reduce</function> 将回调函数
|
||
<parameter>callback</parameter> 迭代地作用到
|
||
<parameter>input</parameter> 数组中的每一个单元中,从而将数组简化为单一的值。如果指定了可选参数
|
||
<parameter>initial</parameter>,该参数将被当成是数组中的第一个值来处理,或者如果数组为空的话就作为最终返回值。
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title><function>array_reduce</function> 例子</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
function rsum($v, $w) {
|
||
$v += $w;
|
||
return $v;
|
||
}
|
||
|
||
function rmul($v, $w) {
|
||
$v *= $w;
|
||
return $v;
|
||
}
|
||
|
||
$a = array(1, 2, 3, 4, 5);
|
||
$x = array();
|
||
$b = array_reduce($a, "rsum");
|
||
$c = array_reduce($a, "rmul", 10);
|
||
$d = array_reduce($x, "rsum", 1);
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
这将使 <varname>$b</varname> 的值为
|
||
<literal>15</literal>,<varname>$c</varname> 的值为
|
||
<literal>1200</literal>(= 1*2*3*4*5*10),以及
|
||
<varname>$d</varname> 的值为 <literal>1</literal>。
|
||
</para>
|
||
<para>
|
||
参见 <function>array_filter</function> 和 <function>array_map</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
|
||
-->
|