添加部分php mysql数组一起在小束?省略同一数组的其他部分


Add parts of a php mysql array together in little bunches? Leave out other parts of same array?

我怎样才能改变下面的代码,使每个部分加在一起,而不是挤在一起?如果屏幕上出现的一个小部分是123,它应该将12+3加起来显示15而不是123。我已经尝试过sum_array和其他东西,但它不会工作与其他部分添加零件在小束。我只能让它显示混合在一起的结果,或者以其他方式添加错误的部分或整个东西。

 $data = mysql_query('SELECT weight FROM my_table WHERE session_id = "' . session_id() . '"'); 
    $params = array();
    while ($row = mysql_fetch_assoc($data)) {     
    $params[] = $row['weight']; 
    }
    $combinations=getCombinations($params);
    function getCombinations($array)
    {
        $length=sizeof($array);
        $combocount=pow(2,$length);
    for ($i=1; $i<$combocount; $i++)
        {
    $binary = str_pad(decbin($i), $length, "0", STR_PAD_LEFT);
            $combination='';
            for($j=0;$j<$length;$j++)
            {
                if($binary[$j]=="1")
                    $combination.=$array[$j];
            }
            $combinationsarray[]=$combination;
     echo $combination . "&lt;br&gt;"; 
        }
        return $combinationsarray;
    } 

看起来

$combination.=$array[$j];

是你的问题。在PHP中用于字符串连接而不是数学。因为PHP是一种松散的数据类型语言,你告诉PHP取$array[$j]和"的字符串值。="(追加)它到$combination,得到12 .= 3 == "123"的问题,而不是你想要的15。你应该试试+=

如果我明白你想做什么,我认为你想在下一行使用添加+而不是连接.:

if($binary[$j]=="1")
    $combination += $array[$j];