0

I have those arrays, which contains totals for a monthly report:

Array (
 [name] => Creativo
 [Total] => 98910.44
 [Pedidos] => 89
 [Descuento] => 448.54
 [Clientes] => 11
 [Pliegos] => 1504.100 
)

Array (
 [name] => Emprendedor
 [Total] => 48561.47
 [Pedidos] => 38
 [Descuento] => 0.00
 [Clientes] => 9
 [Pliegos] => 842.400 
) 

Array (
 [name] => Detallista
 [Total] => 30428.87
 [Pedidos] => 163
 [Descuento] => 0.00
 [Clientes] => 22
 [Pliegos] => 107.940 )

Array (
 [name] =>
 [Total] => 1089.98
 [Pedidos] => 3
 [Descuento] => 0.00
 [Clientes] => 2
 [Pliegos] => 10.600 
)

I need a totalized array like this:

Array (
 [Total] => 178990.79
 [Pedidos] => 293
 [Descuento] => 448.54
 [Clientes] => 44
 [Pliegos] => 2465.34
)

I dont know how sum the same keys because the values are in differents arrays. how can I do?

Regards.

araujophillips
  • 322
  • 1
  • 5
  • 22

4 Answers4

1

I think you have 4 different arrays like below

 <?php
    $arr1=array (
     'name' => 'Creativo',
     'Total' => 98910.44,
     'Pedidos' => 89,
     'Descuento' => 448.54,
     'Clientes' => 11,
     'Pliegos' => 1504.100 
    );

    $arr2=array (
     'name' => 'Emprendedor',
     'Total' => 48561.47,
     'Pedidos' => 38,
     'Descuento' => 0.00,
     'Clientes' => 9,
     'Pliegos' => 842.400 
    );

    $arr3=array (
     'name' => 'Detallista',
     'Total' => 30428.87,
     'Pedidos' => 163,
     'Descuento' => 0.00,
     'Clientes' => 22,
     'Pliegos' => 107.940 );

    $arr4=array (
     'name' =>'yyy',
     'Total' => 1089.98,
     'Pedidos' => 3,
     'Descuento' => 0.00,
     'Clientes' => 2,
     'Pliegos' => 10.600, 
    );

    echo "<pre>";
    foreach ($arr1 as $k=>$v) {
        if($k!='name')
            $arr[$k]=$v+$arr2[$k]+$arr3[$k]+$arr4[$k];
        else
            $arr[$k]=$v;    
    }

    print_r($arr);
    ?>
sumit
  • 15,003
  • 12
  • 69
  • 110
0

Something like this could work (this is untested)

$total = 0;
$pedidos = 0;
$descuento = 0;
$clientes = 0;
$pliegos = 0;

foreach($array as $arr) {
    $total = $total + $arr['Total'];
    $pedidos = $pedidos + $arr['Pedidos'];
    $descuento = $descuento + $arr['Descuento'];
    $clientes = $clientes + $arr['Clientes'];
    $pliegos = $pliegos + $arr['Pliegos'];
}

echo $total;

This will iterate through each array and add on to it each time.

Karl
  • 5,435
  • 11
  • 44
  • 70
0

you should use *array_merge* to merge all the arrays that you need into one, then do a foreach loop and sum all the desired elements

gh0st
  • 214
  • 3
  • 15
0

Something like this work for you?

 $totals = array();
 foreach($Report as $item) {
   if(!array_key_exists($item['name'], $totals)) {
     // We haven't come across this name yet, so make a placeholder with 0's
     $totals[$item['name']] = array(
       'name' => $item['name'],
       'Total' => 0,
       'Pedidos' => 0,
       'Descuento' => 0,
       'Clientes' => 0,
       'Pliegos' => 0,
     );
   }

   // Increment the amounts for the specific name
   $totals[$item['name']]['Total'] += $item['Total'];
   $totals[$item['name']]['Pedidos'] += $item['Pedidos'];
   $totals[$item['name']]['Descuento'] += $item['Descuento'];
   $totals[$item['name']]['Clientes'] += $item['Clientes'];
   $totals[$item['name']]['Pliegos'] += $item['Pliegos'];
 }
 print_r($totals);
Sam
  • 20,096
  • 2
  • 45
  • 71