PHP Version
4.0.5
/**
 * Example of using array_reduce() 
 * to sum all elements of an array and produce a single accumulated value
 */

// Array to apply array_reduce() on
$array = [1, 2, 3, 4, 5];

// Callback function written by the developer
function sum($total, $number) {
    // Add the previous accumulated value and the current element
    return $total + $number;
}

// Apply the callback function to all elements and return the single result
$result = array_reduce($array, 'sum');

echo $result; // Output: 15
array_reduce(array $array, callable $callback, mixed $initial = null): mixed
callback(mixed $carry, mixed $item): mixed
/**
 * Callback Function
 *
 * Can be a named function (user-defined) or an anonymous function
 */


// Using a named function as a callback
function callback($carry, $item) { // define named function
    // Logic for accumulating a single value: must return the accumulated result
}

array_reduce($array, 'callback'); // pass the name of the named function as a string

// Using an anonymous function as a callback
array_reduce($array, function($carry, $item) {
    // Logic for accumulating a single value: must return the accumulated result
});
$numbers = [1, 2, 3, 4, 5];

$sum = array_reduce($numbers, function ($carry, $item) {
    return $carry + $item;
});

echo "Sum of all array elements: $sum"; // Output: 'Sum of all array elements: 15'
$numbers = [2, 3, 4, 5];

$product = array_reduce($numbers, function ($carry, $item) {
    return $carry * $item;
}, 1); // Set the initial accumulated value to 1

echo "Product of all array elements: $product"; // Output: 'Product of all array elements: 120'
$numbers = [5, 3, 9, 2, 7];

$min = array_reduce($numbers, function ($carry, $item) {
    return ($item < $carry) ? $item : $carry;
}, $numbers[0]);

echo "Minimum value in the array: $min"; // Output: 'Minimum value in the array: 2'
$numbers = [5, 3, 9, 2, 7];

$max = array_reduce($numbers, function ($carry, $item) {
    return ($item > $carry) ? $item : $carry;
}, $numbers[0]);

echo "Maximum value in the array: $max"; // Output: 'Maximum value in the array: 9'
$words = ['Hello', ' ', 'World', '!'];

$concatenated = array_reduce($words, function ($carry, $item) {
    return $carry . $item;
}, '');

echo "Concatenated array elements: $concatenated"; // Output: 'Concatenated array elements: Hello World!'

// Alternatively, you can use the built-in implode() function
$implode = implode('', $words);
echo $implode;  // Output: 'Hello World!'