• 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 delete old images from public folder on update using Laravel

Array to string conversion

Converting array to string and then back in PHP

Array to string conversion

How to create a cookie to store the timestamp of when a page is first loaded with php

How to install php yaml on CentOs?

Show rotation of tweets using current day of month

Wordpress add responsive srcset header image to theme

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

How to remove from a multidimensional array all duplicate elements including the original?

Using array_intersect on a multi-dimensional array

Any AWS EB Laravel route getting 404 Not Found nginx/1.16.1

Laravel Unknown Column 'updated_at'

Get sql.h error on MacOS 12 (Monterey) sqsrv and pdo_sqsrv drive installation

Backend cannot be reached after Typo3 login screen

About Contact Privacy policy Terms and conditions