<!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>Reduces the set to a single value using a callback function</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ds-set.merge.html">Ds\Set::merge</a></div> <div class="next" style="text-align: right; float: right;"><a href="ds-set.remove.html">Ds\Set::remove</a></div> <div class="up"><a href="class.ds-set.html">Set</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="ds-set.reduce" class="refentry"> <div class="refnamediv"> <h1 class="refname">Ds\Set::reduce</h1> <p class="verinfo">(PECL ds >= 1.0.0)</p><p class="refpurpose"><span class="refname">Ds\Set::reduce</span> — <span class="dc-title">Reduces the set to a single value using a callback function</span></p> </div> <div class="refsect1 description" id="refsect1-ds-set.reduce-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>Ds\Set::reduce</strong></span> ( <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></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">$initial</code></span> ] )</div> <p class="para rdfs-comment"> Reduces the set to a single value using a callback function. </p> </div> <div class="refsect1 parameters" id="refsect1-ds-set.reduce-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <code class="parameter">callback</code></dt> <dd> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><span class="replaceable">callback</span></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">$carry</code></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">$value</code></span> )</div> <dl> <dt> <code class="parameter">carry</code></dt> <dd> <p class="para"> The return value of the previous callback, or <code class="parameter">initial</code> if it's the first iteration. </p> </dd> <dt> <code class="parameter">value</code></dt> <dd> <p class="para"> The value of the current iteration. </p> </dd> </dl> </dd> <dt> <code class="parameter">initial</code></dt> <dd> <p class="para"> The initial value of the carry value. Can be <strong><code>NULL</code></strong>. </p> </dd> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-ds-set.reduce-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> The return value of the final callback. </p> </div> <div class="refsect1 examples" id="refsect1-ds-set.reduce-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-5318"> <p><strong>Example #1 <span class="function"><strong>Ds\Set::reduce()</strong></span> with initial value example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$set </span><span style="color: #007700">= new \</span><span style="color: #0000BB">Ds</span><span style="color: #007700">\</span><span style="color: #0000BB">Set</span><span style="color: #007700">([</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">]);<br /><br /></span><span style="color: #0000BB">$callback </span><span style="color: #007700">= function(</span><span style="color: #0000BB">$carry</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> return </span><span style="color: #0000BB">$carry </span><span style="color: #007700">* </span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br />};<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$set</span><span style="color: #007700">-></span><span style="color: #0000BB">reduce</span><span style="color: #007700">(</span><span style="color: #0000BB">$callback</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Iterations:<br />//<br />// $carry = $initial = 5<br />//<br />// $carry = $carry * 1 = 5<br />// $carry = $carry * 2 = 10<br />// $carry = $carry * 3 = 30<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> int(30) </pre></div> </div> </div> <div class="example" id="example-5319"> <p><strong>Example #2 <span class="function"><strong>Ds\Set::reduce()</strong></span> without an initial value example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$set </span><span style="color: #007700">= new \</span><span style="color: #0000BB">Ds</span><span style="color: #007700">\</span><span style="color: #0000BB">Set</span><span style="color: #007700">([</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">]);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$set</span><span style="color: #007700">-></span><span style="color: #0000BB">reduce</span><span style="color: #007700">(function(</span><span style="color: #0000BB">$carry</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> return </span><span style="color: #0000BB">$carry </span><span style="color: #007700">+ </span><span style="color: #0000BB">$value </span><span style="color: #007700">+ </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br />}));<br /><br /></span><span style="color: #FF8000">// Iterations:<br />//<br />// $carry = $initial = null<br />//<br />// $carry = $carry + 1 + 5 = 6<br />// $carry = $carry + 2 + 5 = 13<br />// $carry = $carry + 3 + 5 = 21<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> int(21) </pre></div> </div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="ds-set.merge.html">Ds\Set::merge</a></div> <div class="next" style="text-align: right; float: right;"><a href="ds-set.remove.html">Ds\Set::remove</a></div> <div class="up"><a href="class.ds-set.html">Set</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>