• 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

Inserting Country Selection into MySQL PHP [duplicate]

Explode not working properly with dash

How to unlink image from folder?

Can we define variables in `.tpl` files?

How to separate letters and digits from a string in php

WordPress ACFNotice: get_field() - We've detected one or more calls to retrieve ACF field values before ACF has been initialized

Insert database rows from columns of data from associative array of indexed arrays

get attribute from class not working php 7

Which is faster php date functions or carbon?

OAuth2 Token PHP

protect my blog content

How to change product Image when variables are selected in Shop and Archive Pages?

Unable to uninstall brew php from homebrew

Sagepay Error The Vendor failed to provide a RedirectionURL

Codeigniter, Severity: error --> Exception: Too few arguments to function, admin dashboard shows HTTP error 500

About Contact Privacy policy Terms and conditions