• 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

How to make a foreign key not using primary key

Docker image build with PHP zip extension shows "bundled libzip is deprecated" warning

Make survey at laravel 5.4 and MySQL

I want to display default profile image if user do not have profile image

Yii2: How do I debug console commands?

How to get monthly wise data in laravel

Check for PDO Results, If None Display Message, If Yes then Loop Through

How to run or debug php on Visual Studio Code (VSCode)

SendGrid: Sending an email to multiple recipients without other emails being shown on the "to" field

Inserting data into SQL Server Db An Invalid direction was specified

Split comma separated value from table column into rows using mysql?

Detect emoticons in string

How to increase the PHP upload limits [duplicate]

Warning: A non-numeric value encountered

Getting public posts of a random user from Facebook API

About Contact Privacy policy Terms and conditions