• 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

WordPress Security Standards want me to escape my html, but how to do it correctly?

Remove country code from phone number?

enable Apache http Authorization header

Laravel - Implicit route model binding with soft deleted data

Google Gmail API - How to login programatically?

Best way to scrolldown onpageload

possible options to create pdf file using html elements to generate invoice in php and codeigniter

Transform array, set each array element with parent key php

Magento 2 - How to add new block to admin panel in product page?

SQL to convert multiple rows into a single row of variable length

woocommerce change position of price

Don't show recurring price for WooCommerce subscriptions worth 0$

Hide email address with stars (*)

How to get AJAX to post on second page?

Querying only one row from a one to many relationship laravel

About Contact Privacy policy Terms and conditions