D
D
Denis2021-07-14 12:47:19
PHP
Denis, 2021-07-14 12:47:19

Can't figure out how to write a php function?

This is what it looks like so far

)->reduce(function ($carry, $item) {
    $startCount = 0;

    foreach ($item as $value) {
        // вычисления
    }

    $startCount = $result; // в $result вычисления после foreach лежат
    $carry[] = $result;
    return $carry;
}, []);

on the first iteration before foreach $startCount = 0; I need that starting from the second iteration of reduce , the $startCount variable has the value $result; is it possible to implement this? Otherwise, it's $startCount; reset every time...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2021-07-14
@Drumsid

You are doing something strange.
The result of the previous iteration is contained in $carry.
The result of the current iteration must be returned with return
Why do you need $startCount?
If you really need an "external" variable, you can fasten it like this https://ideone.com/n0nBVZ
If you are converting one array to another, then perhaps not reduce, but array_map is more suitable for you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question