Open In App

PHP | Ds\Sequence reduce() Function

Last Updated : 22 Aug, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The Ds\Sequence::reduce() function is an inbuilt function in PHP which is used to reduce the sequence to a single value using a callback function. Syntax:
mixed abstract public Ds\Sequence::reduce ( callable $callback 
[, mixed $initial ] )
Parameters: This function accepts two parameters as mentioned above and described below:
  • $callback: This parameter holds the function which contains the operation on the elements and store carry. This callback function contains two arguments, carry and value, where carry is the value returned by the function and value is the value of element at the current iteration.
  • $initial: This parameter holds the initial value of carry, which can be NULL.
Return value: This function returns the final value of callback function. Below programs illustrate the Ds\Sequence::reduce() function in PHP: Program 1:
Output:
Sequence Elements
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Element after performing operation
int(25)
Program 2:
Output:
Original sequence elements
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)

Sequence after reducing to single element
int(120000000)
Reference: https://www.php.net/manual/en/ds-sequence.reduce.php

Next Article

Similar Reads