• 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

CONCAT columns with Laravel 5 eloquent

stay with the last 15 elements of an array [duplicate]

The "client_reference_id" argument is not passed

Laravel php artisan serve to mimic HTTPS

how to get the header value, if we don't know the value because the value is random from the server

How to add a heading in between checkout fields of WooCommerce

Combine array with same value and add other [duplicate]

Transpose csv file data [duplicate]

Laravel after login Two factor not working

Maatwebsite Excel 3.1 : how do I skip duplicate data when imported?

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

How to override htaccees file for cache control header

How to pass data to all views in Laravel 5?

Invalid value in field "itemtype" in Google Search Console

Combine array with same value and add other [duplicate]

About Contact Privacy policy Terms and conditions