• 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

Check If array is null or not in php

Is there a way to correctly use sanitize_text_field and wp_unslash that doesn't cause psalm to error with "expects string, possibly different type"

str_word_count() function doesn't display Arabic language properly

Laravel 5.1 Unknown database type enum requested

Get number of working days between two dates in PHP [duplicate]

Testing subscription renewals on Stripe

Transpose csv file data [duplicate]

Execute only one time and then wait set period of time before executing again

Laravel 8 Multiple Relationships for Factory

PhP how to calculate moments with variables rows

Comma separated list from array with "and" before last element

Show only featured products in Woocommerce shop page

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

Increase value by 1 on button click

How to pass security cloudflare server with php curl

About Contact Privacy policy Terms and conditions