• Categories
    • PHP
    • phpMyAdmin
    • PHPMailer
    • FFMpeg
    • PHPEXcel
    • PHPDoc
    • PHPUnit
    • CakePHP
    • CakePHP 2.0
    • Cake PHP 2.1
    • CakePHP Model
    • Facebook PHP SDK
    • composer-php
    • PHP 7
    • PHP GD
    All Categories
  • About

Sum array values of a column within each column of an array with 3 levels

phparraysmultidimensional-arraytransposearray-sum


PHP Snippet 1:

foreach($array as $key => $point){
  $arr[] = array_sum(array_column( array_column($array,$key),'value'));
}
print_r($arr);

PHP Snippet 2:

<?php

$arr = [];
foreach($array as $point){
   foreach($point as $k => $v){
      $arr[$k] = ($arr[$k] ?? 0) + $v['value'];
   }
}
print_r($arr); 

PHP Snippet 3:

var_export(
    array_map(
        fn(...$col) => array_sum(array_column($col, 'value')),
        ...$array
    )
);

PHP Snippet 4:

array (
  0 => 3600,
  1 => 7000,
  2 => 6000,
)

Related Snippets

Custom post type single page not working

PHPExcel file download using AJAX call

PHP drop down list using array's and foreach (else and for) code

How can I get user id from session in javascript?

cakephp save drag and drop list order to database

PHP7 CLI attempts to load xdebug twice - "Cannot load Xdebug - it was already loaded"

merge all files in directory to one text file

mysql slow on updates for a well optimized query

ldap is missing from system when installing adldap2 in laravel

Laravel Unknown Column 'updated_at'

PHP array, move keys and values to new array, but mix it up

JSON Render Issue in Date Object Laravel and PHP 7.4

PHP, how to convert Int value to Week days

Target Individual Form Instance/Counter Inside A PHP While Loop

enable Apache http Authorization header

About Contact Privacy policy Terms and conditions