<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.array.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'function.array-reduce.php',
    1 => 'array_reduce',
    2 => 'Iteratively reduce the array to a single value using a callback function',
  ),
  'up' => 
  array (
    0 => 'ref.array.php',
    1 => 'Функції для роботи з масивами',
  ),
  'prev' => 
  array (
    0 => 'function.array-rand.php',
    1 => 'array_rand',
  ),
  'next' => 
  array (
    0 => 'function.array-replace.php',
    1 => 'array_replace',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/array/functions/array-reduce.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.array-reduce" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_reduce</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.5, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">array_reduce</span> &mdash; <span class="dc-title">Iteratively reduce the array to a single value using a callback function</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.array-reduce-description">
  <h3 class="title">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_reduce</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$array</code></span>, <span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$initial</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>array_reduce()</strong></span> applies iteratively the
   <code class="parameter">callback</code> function to the elements of the
   <code class="parameter">array</code>, so as to reduce the array to
   a single value.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.array-reduce-parameters">
  <h3 class="title">Параметри</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">array</code></dt>
     <dd>
      <p class="para">
       The input array.
      </p>
     </dd>
    
    
     <dt><code class="parameter">callback</code></dt>
     <dd>
      <div class="methodsynopsis dc-description">
       <span class="methodname"><span class="replaceable">callback</span></span>(<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$carry</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$item</code></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

      <dl>
       
        <dt><code class="parameter">carry</code></dt>
        <dd>
         <p class="para">
          Holds the return value of the previous iteration; in the case of the
          first iteration it instead holds the value of 
          <code class="parameter">initial</code>.
         </p>
        </dd>
       
       
        <dt><code class="parameter">item</code></dt>
        <dd>
         <p class="para">
          Holds the value of the current iteration.
         </p>
        </dd>
       
      </dl>
     </dd>
    
    
     <dt><code class="parameter">initial</code></dt>
     <dd>
      <p class="para">
       If the optional <code class="parameter">initial</code> is available, it will
       be used at the beginning of the process, or as a final result in case
       the array is empty.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.array-reduce-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="para">
   Returns the resulting value.
  </p>
  <p class="para">
   If the array is empty and <code class="parameter">initial</code> is not passed,
   <span class="function"><strong>array_reduce()</strong></span> returns <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.array-reduce-changelog">
  <h3 class="title">Журнал змін</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Версія</th>
      <th>Опис</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
 <td>8.0.0</td>
 <td>
  Тепер функція видасть <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>, якщо
  <code class="parameter">callback</code> очікує параметр за посиланням.
 </td>
</tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.array-reduce-examples">
  <h3 class="title">Приклади</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Приклад #1 <span class="function"><strong>array_reduce()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">sum</span><span style="color: #007700">(</span><span style="color: #0000BB">$carry</span><span style="color: #007700">, </span><span style="color: #0000BB">$item</span><span style="color: #007700">)<br />{<br />    </span><span style="color: #0000BB">$carry </span><span style="color: #007700">+= </span><span style="color: #0000BB">$item</span><span style="color: #007700">;<br />    return </span><span style="color: #0000BB">$carry</span><span style="color: #007700">;<br />}<br /><br />function </span><span style="color: #0000BB">product</span><span style="color: #007700">(</span><span style="color: #0000BB">$carry</span><span style="color: #007700">, </span><span style="color: #0000BB">$item</span><span style="color: #007700">)<br />{<br />    </span><span style="color: #0000BB">$carry </span><span style="color: #007700">*= </span><span style="color: #0000BB">$item</span><span style="color: #007700">;<br />    return </span><span style="color: #0000BB">$carry</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= array(</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">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$x </span><span style="color: #007700">= array();<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_reduce</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #DD0000">"sum"</span><span style="color: #007700">)); </span><span style="color: #FF8000">// int(15)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_reduce</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #DD0000">"product"</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">)); </span><span style="color: #FF8000">// int(1200), because: 10*1*2*3*4*5<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_reduce</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">, </span><span style="color: #DD0000">"sum"</span><span style="color: #007700">, </span><span style="color: #DD0000">"No data to reduce"</span><span style="color: #007700">)); </span><span style="color: #FF8000">// string(17) "No data to reduce"<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-reduce-seealso">
  <h3 class="title">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.array-filter.php" class="function" rel="rdfs-seeAlso">array_filter()</a> - Filters elements of an array using a callback function</span></li>
    <li><span class="function"><a href="function.array-map.php" class="function" rel="rdfs-seeAlso">array_map()</a> - Застосовує зворотній виклик до всіх елементів зазначених масивів</span></li>
    <li><span class="function"><a href="function.array-unique.php" class="function" rel="rdfs-seeAlso">array_unique()</a> - Removes duplicate values from an array</span></li>
    <li><span class="function"><a href="function.array-count-values.php" class="function" rel="rdfs-seeAlso">array_count_values()</a> - Counts the occurrences of each distinct value in an array</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>