• 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

laravel controller function parameters

How to create a video stream from a single dynamic image in PHP

Laravel passport auth token not working after moving to different server

Saving Data from form to database using AngularJS and php

500 (Internal Server Error) with Laravel & Docker [duplicate]

PHP's array_map including keys

Handling expired token in Laravel

How to Display Data in Yajra Datatables Laravel 7?

zsh: command not found: php

dompdf and img tag, image wont show

how to pass row id in href of a tag in codeigniter controller?

Transpose and flatten multiple rows of array data [duplicate]

cakephp save drag and drop list order to database

Whats the point of running Laravel with the command 'php artisan serve'?

PHP contact form configuration [duplicate]

About Contact Privacy policy Terms and conditions