• 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

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

Batch request Google Calendar php API

Laravel Nova Dependency Container what are the allowed resource properties other than id in dependsOn

How to send image,audio or video through the WhatsApp API - PHP

Problems getting instance of UploadedFile in Yii2

How to get quarter for future date using Carbon?

Laravel websockets AWS EC2 - Connection failed

Smarty if isset

No result using makeStyles Material UI in react 18

Add customer email and phone in "Order" column to admin orders list on Woocommerce

Laravel MSSQL Server Connection not working

PHP mail sending empty mails

Get sum of arrays inside array

Dynamic table in HTML using MySQL and php

Merge column values from two arrays to form an indexed array of associative arrays

About Contact Privacy policy Terms and conditions