Who can help explain the mechanism of the function array_reduce http://php.net/array_reduce

I have a description that is in the docks "array_reduce () iteratively applies the callback function of the callback to the elements of the array array and, thus, reduces the array to a single value." don't understand at all.

What does it mean "driving" are other values ​​discarded? The principle of information is also not clear, which one leaves the largest, the smallest and why?

For example, in the code below:

$array = array( 1, 2, 'tom', '7jerry', 010, '020' ); $callback = function ($a, $b) { return $a + intval($b); }; echo array_reduce($array, $callback, 1); 

The answer will be 39, but why and how it turns out is not clear.

    1 answer 1

    In your collection, $a is the total value, $b is the current item. The operation of the function itself is return $a + intval($b); adds to the current sum the value of $b reduced to int 'y. The very same function array_reduce applies your callback to each element and accumulates the total value.

    In steps:

      ab 1) 1 1 2) 2 2 3) 4 tom 4) 4 7jerry 5) 11 8 6) 19 020 

    Here a small embarrassment can only be with 7jerry - when trying to get an int will give out 7. Therefore, at step 5, the amount will be equal to 11.

    After all the steps in the end will be 39.

    • The fact that 7jerry will come to 7 I knew. But about 020 more interesting. Is this an octal numbering system? But the int number resulted in a binary simply by dropping the first 0? Those. it turns out that in this case array_reduce is the sum of all elements since summation is set in callback functions, I understand correctly? - RoboNoob
    • @RoboNoob in this case, yes, it's just the sum of all the values. - Suvitruf