• 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

Return multiple values from a function using mysqli_fetch_assoc [closed]

Pagekite error: FAIL: localhost:8080 is down

phpMyAdmin ERROR: mysqli_real_connect(): (HY000/1045): Access denied for user 'pma'@'localhost' (using password: NO)

How can I create a Download Page with post php method?

Remove product downloads section in woocommerce email notifications

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

Yii Ajax Submit button

PDOException SQLSTATE[HY000] [2002] No such file or directory

PHP if in_array() how to get the key as well?

Project structure for PHP

If action is on a different page do I use fwrite function

Boolean assignment operators in PHP

How to debug in WooCommerce 3+

AJAX call fails with SyntaxError: Unexpected end of JSON input

Eloquent insert id with sequence next value

About Contact Privacy policy Terms and conditions