• 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

Prestashop webservice API creating cart rules

Elasticsearch - Want to sort by field in all indices where that particular field available or not if not then avoid it

How to find the date a user created their Google account

How can I get a div content in php

Fatal error: [] operator not supported for strings

MySQL order by field in Eloquent

Message: Set sys_temp_dir in your php.ini after installed composer

phpMyAdmin ERROR: mysqli_real_connect(): (HY000/1045): Access denied for user 'pma'@'localhost' (using password: NO)

PHP -> Next nearest date defined by array of days in week

Call to a member function givePermissionTo() on null

VB.NET WebRequest with PHP POST

Telegram bot doesn't answer

Converting array to string and then back in PHP

How to Generate PDF invoice to XML format using mPDF

Artisan, creating tables in database

About Contact Privacy policy Terms and conditions