> For the complete documentation index, see [llms.txt](https://arrayutils.docs.present.kim/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://arrayutils.docs.present.kim/methods/g/reduce/right.md).

# reduceRight()

{% code title="Example.php" %}

```php
<?php use kim\present\utils\arrays\ArrayUtils;

$arrayUtils = ArrayUtils::from(range(1, 10));

//This is like a 10 factorial
$arrayUtils->reduceRight(function($accumulator, $value){ return $accumulator * $value; }, 1);
// expected output: 3628800



$arrayUtils->reduceRight(
    function($accumulator, $value){
        echo  "$accumulator * $value = " . ($accumulator * $value) . PHP_EOL;
        return $accumulator * $value; }, 1));
//echo 1 * 10 = 10
//echo 10 * 9 = 90
//echo 90 * 8 = 720
//echo 720 * 7 = 5040
//echo 5040 * 6 = 30240
//echo 30240 * 5 = 151200
//echo 151200 * 4 = 604800
//echo 604800 * 3 = 1814400
//echo 1814400 * 2 = 3628800
//echo 3628800 * 1 = 3628800
//expected output: 3628800
```

{% endcode %}

## Syntax

```php
$arrayUtils->reduceRight(callable $callback, mixed $initialValue = null) : mixed;
```

### Parameter

* `$callback`

  > A function that produces an element of the new Array, taking four arguments:
  >
  > * `$accumulator`The accumulator accumulates callback's return values. It is the accumulated value previously returned in the last invocation of the callback—or `$initialValue`, if it was supplied (see below).
  > * `$value` The current element being processed in the array.
  > * `$key` The index of the current element being processed in the array.
  > * `$array`  The array `every` was called upon.
* `$initialValue` <img src="/files/-MKk9YjCz_YkVR1YiRy-" alt="" data-size="line">&#x20;
  * A value to use as the first argument to the first call of the callback.

### Return value

* The result value.

## Prefixing

```php
ArrayUtils::reduceRightFrom(iterable $from, callable $callback, mixed $initialValue = null) : mixed;
```

## References

{% content-ref url="/pages/-MKmSpThy4vgovSVrqFM" %}
[reduce()](/methods/g/reduce.md)
{% endcontent-ref %}

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/ReduceRight>" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://arrayutils.docs.present.kim/methods/g/reduce/right.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
