• 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

Could not decode a text frame as UTF-8.

Distribute array row data to make multiple new rows

Converting IPv6 to IPv4 address in PHP

Get latest Tweets - What API

Ajax GET request fails in laravel 8

Toggle between a textbox and a select using PHP

How to prevent phpmailer sending embedded image as an attachment on Gmail?

Why rand() every time I refresh the page?

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

php: command not found Command PhaseScriptExecution failed with a nonzero exit code

Add custom text under order details on WooCommerce My account view order pages

Laravel eloquent update record without loading from database

How to unlink image from folder?

"Mixed content blocked" when running an HTTP AJAX operation in an HTTPS page

How to use React Router with Laravel?

About Contact Privacy policy Terms and conditions