【问题标题】:I want to summarize on a table我想在一张桌子上总结
【发布时间】:2017-04-14 16:52:19
【问题描述】:
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 是标识符。我想将所有具有相同标识符的权重相加。帮助。提前谢谢你!

【问题讨论】:

  • 此数据是否从 SQL 数据库中检索?
  • 是的,这些数据存储在我给定数据库的一个表中的数组类型字段中
  • 然后使用您的 SQL 查询通过 SUM() 和 GROUP BY 执行此操作:SELECT idBout, SUM(poidsTot) AS total FROM tablename GROUP BY idBout

标签: php arrays multidimensional-array


【解决方案1】:

$newarr 是一个新的 sum 数组

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

导致:

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

https://eval.in/776652

【讨论】:

  • 谢谢我这样做了 $cetteTarifica = $content['tarification']; $newarr = 数组(); foreach($cetteTarifica as $onea) { $newarr[$onea['idBout']]+= $onea['poidsTot']; } print_r($newarr);死();
  • 确保您的数组结构正确并匹配 foreach 循环中的值。如果可能的话,给我看完整的代码(使用 eval.in,这很好)
  • $em = $this->getDoctrine()->getManager(); $thisCommande = $em->getRepository('OCCommandesBundle:Commandes')->find($commande->getContent()); $content = $thisCommande->getCommande(); $cetteTarifica = $content['tarification']; $newarr = 数组(); foreach($cetteTarifica as $onea) { $newarr[$onea['idBout']]+=$onea['poidsTot']; } print_r($newarr);死();
  • 这是一个 symfony3 项目
  • 我要看看$cetteTarifica数组的结构,请用eval.in给我看。
猜你喜欢
  • 2017-04-19
  • 1970-01-01
  • 1970-01-01
  • 2017-08-21
  • 1970-01-01
  • 2014-10-14
  • 1970-01-01
  • 2012-05-23
  • 1970-01-01
相关资源
最近更新 更多