• 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

How to reset Laravel AuthManager/guards in between API calls in tests?

Display the WooCommerce product price inside a custom function

PHP contact form configuration [duplicate]

Transpose and flatten multiple rows of array data [duplicate]

I want to store data in new index as per my key in php [duplicate]

How to find the date a user created their Google account

Printing more than one array using print_r or any other function in php

How can I stop a symfony process which is listening on http://127.0.0.1:8000

Laravel query builder binding parameters more than once

How to redirect to another page and call a Function there on Angular ng-click

wordpress : How to specify the cause "This site can’t be reached"

Adding the custom page with add_menu_page function on Wordpress

Laravel Pagination links() not working

OctoberCMS / Anonymous Global Scope

"cannot list resources" error from WooCommerce REST API

About Contact Privacy policy Terms and conditions