• 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

add " ? " in url via htaccess RewriteRule

PHP Download MP3 files from directory on server

Get Header Authorization value of a login api (sent with username and password in json body) using php curl

php 7 php.ini upload_max_filesize not working

target [LaravelFortifyContractsRegisterViewResponse] is not instantiable

xdebug 3 not working in ubuntu 20.04 with docker

PHP rotate matrix counter-clockwise

How to trim a video by 4 fragments to 4 seconds using the PHP-FFMpeg?

php curl requesting twice

How to use functions that are declared in snippets in Evolution CMS (Modx) in an external PHP Script?

How to override header set in Apache config with more specific header in a virtual host

backup files to google drive using PHP

How to skip cart page on woocomerce for certain products only?

Creating live search with AJAX and CodeIgniter

Star rating with half star support

About Contact Privacy policy Terms and conditions