Using 'reduce' for summing objects property in array - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of Using 'reduce' for summing objects property in array.php
- <?php
- $commissionsArray = [
- (object) [
- 'name' => 'jon',
- 'commission' => 5
- ],
- (object) [
- 'name' => 'smith',
- 'commission' => 1
- ],
- (object) [
- 'name' => 'philip',
- 'commission' => 8
- ],
- ];
- $total = array_reduce($commissionsArray, function ($sum, $entry) {
- $sum += $entry->commission;
- return $sum;
- }, 0);
- echo "Total: $total\n";
File Description
- Using 'reduce' for summing objects property in array
- PHP Code
- 22 Mar-2021
- 367 Bytes
You can Share it:
Latest PHP Pastes