2017-04-14 59 views
1
Array ( 
    [1] => Array ( 
     [poidsTot] => 1 
     [idBout] => 1 
    ) 
    [2] => Array ( 
     [poidsTot] => 2 
     [idBout] => 1 
    ) 
    [3] => Array ( 
     [poidsTot] => 2 
     [idBout] => 2 
    ) 
    [4] => Array ( 
     [poidsTot] => 8 
     [idBout] => 2 
    ) 
) 

这是一张表,其中包含几个数组,每个数组有两个值。 value1是一个权重,value2是一个标识符。我想用相同的标识符对所有权重进行求和。帮帮我。先谢谢你!我想总结一张桌子

+1

正在从一个SQL数据库中检索这些数据? –

+0

是这些数据存储在我的数据库的其中一个表中的数组类型字段中 –

+0

然后使用您的SQL查询通过SUM()和GROUP BY执行此操作:SELECT idBout,SUM(poidsTot)AS total FROM tablename GROUP BY idBout' –

回答

0

$ newarr是总和

$newarr = array(); 
foreach($a as $onea) 
{ 
    $newarr[$onea['idBout']]+= $onea['poidsTot']; 
} 

的新数组导致:

Array 
(
    [1] => 3 
    [2] => 10 
) 

https://eval.in/776652

+0

谢谢我这样做$ cetteTarifica = $ content ['tarification']; \t \t $ newarr = array(); \t \t的foreach($ cetteTarifica美元ONEA) \t \t { \t \t \t $ newarr [$ ONEA [ 'idBout']] + = $ ONEA [ 'poidsTot']; \t \t} \t \t的print_r($ newarr); \t \t die(); –

+0

但我有错误注意:未定义抵消:1 –

+0

确保您的数组是peroperly结构,并在foreach循环中的值相匹配。如果可能,请告诉我完整的代码(使用eval.in,这很好) – Arbels

相关问题