Using 'array_sum' for summing arrays entry 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.

Name: Using 'array_sum' for summing arrays entry in array fullscreencopydownloadembedprint


Your result can be seen below.

Result of php executing





Full code of Using 'array_sum' for summing arrays entry in array.php

  1. <?php
  2. $commissionsArray = [
  3.   [
  4.     'name' => 'jon',
  5.     'commission' => 5
  6.   ],
  7.   [
  8.     'name' => 'smith',
  9.     'commission' => 1
  10.   ],
  11.   [
  12.     'name' => 'philip',
  13.     'commission' => 8
  14.   ],
  15.  
  16. ];
  17.  
  18.  
  19. $total = array_sum(array_column($commissionsArray, 'commission'));
  20.  
  21. echo "Total: $total\n";
  22.  
File Description
  • Using 'array_sum' for summing arrays entry in array
  • PHP Code
  • 22 Mar-2021
  • 287 Bytes
You can Share it: