douye2111 2013-05-07 01:57
浏览 217
已采纳

如何对表PHP上的每一列求和

I want to sum each column on this table:

$docid = array(1, 2, 3, 4, 5);
        $table = array('technology' => 1 , 'languange' => 2, 'town' => 3, 'gadget' => 4, 'smartphone' => 5);
        echo "<table><tr><th>Token/Document</th>";
        $count = count($table);
        $doc_count = count($docid);
        for($i=1; $i<=$count; $i++)
        {
            echo "<th>Doc.$i</th>";
        }
        foreach($table as $key=>$value)
        {
            echo "<tr><td>$key</td>";
            for($i=0; $i<$doc_count;$i++)
            {
                $random = rand(1, 8);
                echo "<td>$random</td>";
            }
        }
        echo "</tr><td>RESULT</td>";
        for($i=0; $i<$doc_count;$i++)
        {
            echo "<td>...(what should i use?)...</td>";
        }
        echo "</tr></table>";

Example:

=============================================

| Token/Document | Doc.1 | Doc.2 | Doc.3 | Doc.4 | Doc.5 |

=============================================

| technology...........|....5.....|....7.....|....4.....|....2.....|....1.....|

| language.............|....6.....|....8.....|....1.....|....5.....|....5.....|

| town....................|....5.....|....3.....|....2.....|....7.....|....6.....|

| gadget.................|....4.....|....1.....|....2.....|....4.....|....8.....|

| smartphone.........|....3.....|....7.....|....3.....|....1.....|....2.....|

=============================================

RESULT...............|....23....|....26....|....12....|....19....| 22....|

How to sum each column?

  • 写回答

2条回答 默认 最新

  • doujiao8491 2013-05-07 02:29
    关注

    You need to store each $random so you can total them in the final row.

    $random = rand(1, 8);
    $results[$i][] = $random;  // store $random value in array
    

    Then total the array using array_sum()

    echo "<td>".array_sum($results[$i])."</td>";
    

        $docid = array(1, 2, 3, 4, 5);
        $table = array('technology' => 1 , 'languange' => 2, 'town' => 3, 'gadget' => 4, 'smartphone' => 5);
        echo "<table><tr><th>Token/Document</th>";
        $count = count($table);
        $doc_count = count($docid);
        for($i=1; $i<=$count; $i++)
        {
            echo "<th>Doc.$i</th>";
        }
        foreach($table as $key=>$value)
        {
            echo "<tr><td>$key</td>";
            for($i=0; $i<$doc_count;$i++)
            {
                $random = rand(1, 8);
                $results[$i][] = $random; // save in array for totaling
                echo "<td>$random</td>";
            }
        }
        echo "</tr><td>RESULT</td>";
        for($i=0; $i<$doc_count;$i++)
        {
            echo "<td>".array_sum($results[$i])."</td>"; // total array using array_sum()
        }
        echo "</tr></table>";
    

    phpfiddle - http://phpfiddle.org/main/code/psy-ejm

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址