If you want to get get sum of values in specific index of multidimensional array using php then check this code which help you.

<?php
$items = array(
 array(1 => 1, 2 => 'White Shirt', 3 => 2),
 array(1 => 2, 2 => 'Blue Shirt', 3 => 3)
);
echo "<pre>";
print_r($items);
 
echo array_sum(array_column($items, 3)); // output 5
 
//it will work for php 5.5+
 // PHP 5.5+
 echo array_sum(array_column($array, 'yourindexname')); // 
 
// PHP 4+
function sumArray($item) {
return $item['yourindex'];
}
 
echo array_sum(array_map('sumArray', $array));
 
// PHP 5.3+
echo array_sum(array_map(
function($item) {
    return $item['yourindex'];
}, $items)
);
 
 
 
$sumvalue = array();
 
foreach ($array as $k=>$sub_array) {
  foreach ($sub_array as $id=>$value) {
  $sumvalue [$id]+=$value;
}
}
 
 print_r($sumvalue );

if you face any problem then inform me.

View 1 Comment