• 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

Group rows by column and sum another column within groups [duplicate]

Submitting a form with ajax in Wordpress

Magento 2 - Controller returning blank page

Apply session id from request header

PHP array sort and remove duplicates by two field values

Losing session data after POST from third party website

PHP Startup Unable to load dynamic library /usr/lib/php/20151012/php_mysqli.dll

Error when uploading certain .png files "Interlace handling should be turned on when using png_read_image"

Laravel 4 Redirect::back() not going to previous page (refreshing current page)

Live search query using JS and PHP for QA forum

Put content of wordpress page inside div

Check if a string contain multiple specific words

Using array_intersect on a multi-dimensional array

How can I convert from a{a{} b{}} to a[a[],b[]] using php preg_match?

php script to delete files older than 24 hrs, deletes all files

About Contact Privacy policy Terms and conditions