2014-01-06 36 views
0

我有這些數組,其中包含用於彙總月度報告:PHP的總和

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 
) 

我需要將累計數組是這樣的:

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

我不知道如何總結相同的鍵,因爲值在不同的數組中。我能怎麼做?

問候。

+0

我覺得他的問題是有點不同,他有4個不同的陣列 – sumit

+0

沒錯。 4個陣列是不同的。 – nandophillips

回答

0

像這樣的東西可以工作(這是未經測試)

$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; 

這將通過每個陣列迭代並在每一次添加到它。

+0

它增加了6次相同的值。 – nandophillips

+0

不知道爲什麼,我剛剛使用了'$ array = array(array('Total'=> 2),array('Total'=> 4));'並運行它,$ total給了我'6'正確答案。 – Karl

0

你應該使用* array_merge *合併,你需要進入一個所有的數組,然後做一個的foreach循環,總結所有所需的元素

0

像這樣的事情對你的工作?

$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); 
+0

數組名稱爲$ Report – nandophillips

+0

已更新@nandophillips。 – Sam

+0

['Name']是一個關鍵,等於Total,Pedidos,Descuento,Clientes和Pliegos。 – nandophillips

1

我覺得你有4個不同的排列像下面

<?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); 
    ?>