In PHP, how do I reduce objects in vanilla PHP?

In PHP, you can reduce arrays (objects) using the array_reduce function. This function iteratively reduces an array to a single value.

Keywords: PHP, array_reduce, reduce objects, vanilla PHP
Description: This example demonstrates how to reduce an array of objects to a single value using PHP's array_reduce function.

 1],
    (object) ['value' => 2],
    (object) ['value' => 3],
];

$result = array_reduce($objects, function($carry, $item) {
    return $carry + $item->value;
}, 0);

echo $result; // Output: 6
?>
    

Keywords: PHP array_reduce reduce objects vanilla PHP