NOVINKA: Najžiadanejšie rekvalifikačné kurzy teraz s 50% zľavou + kurz AI ZADARMO. Nečakaj, táto ponuka dlho nevydrží! Zisti viac:

PHP - array_reduce

Trieda

Koreň \ Bez triedy

Metóda - array_reduce

(PHP 4 >= 4.0.5, PHP 5, PHP 7)

Iteratively reduces an array to a single value using a given callback function.

Procedurálne

Parametre

NázovDátový typPredvolená hodnotaPopis
$arrayarray

The input array to reduce.

$callbackcallable

The callback function to reduce the items.

  • $carry - The value of so far reduced items. If the actual iteration is the first one, it contains an initial value.
  • $item - The value of the current element (of the current iteration).

Mávratovej hodnoty

Vracia: mixed

Returns the result after the reduce operation. If the array is empty and no initial value is passed, returns null.

Príklady

<?php
function sum($sumValue, $value)
{
    return $sumValue+ $value;
}

$numbers = array(1, 2, 3, 4, 5);
$nothing = array();

var_dump(array_reduce($numbers, "sum"));
var_dump(array_reduce($numbers, "sum", 10));
var_dump(array_reduce($nothing, "sum", "Initial value"));

Súvisiace manuály

      Aktivity