• 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

Calculating Median of an array in PHP

Convert every two values of an associative array into key-value pairs

send email using gmail-api and google-api-php-client

Multiple order by in WooCommerce

Laravel whole batch is cancelled if one Job fails

Why does array_map() with null as callback create an "array of arrays"?

Uploading video to Youtube using Youtube Data API V3 and Google API Client PHP -- getting 401 (unauthorized) message

PHP rotate matrix counter-clockwise

MySQL default time format UTC or GMT?

Laravel Unresolvable dependency resolving [Parameter #0 [ <required> $method ]] in class GuzzleHttpPsr7Request

Assign output of PHP function to a variable

how to retrieve the first and last instance of a row in pdo dataset

PHPS source file - 403 Forbidden You don't have permission to access this resource

Connecting an HTML webpage to a SQL Server

How to connect to MSSQL Server with PHP from Ubuntu 18.04?

About Contact Privacy policy Terms and conditions