2013-05-07 29 views
0

我想在这个表和每一列和每一列:如何在桌子上PHP

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

例子:

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

|令牌/文件| Doc.1 | Doc.2 | Doc.3 | Doc.4 | Doc.5 |

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

|技术........... | .... 5 ..... | .... 7 ..... | .... 4 ..... | .... 2 ..... | .... 1 ..... |

|语言............. | .... 6 ..... | .... 8 ..... | .... 1 ..... | .. ..5 ..... | .... 5 ..... |

|镇.................... | .... 5 ..... | .... 3 ..... | .... 2。 .... | .... 7 ..... | .... 6 ..... |

|小工具................. | .... 4 ..... | .... 1 ..... | .... 2 .... 。| .... 4 ..... | .... 8 ..... |

|智能手机......... | .... 3 ..... | .... 7 ..... | .... 3 ..... | .... 1。 .... | .... 2 ..... |

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

结果............... | .... 23 .... | .... 26 .... | .... 12 .... | .... 19 .... | 22 .... |

如何求和每列?

回答

1

您需要存储每个$random,以便您可以将它们总计在最后一行中。

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

然后使用总共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

+0

ok..thanks对您有所帮助。但是,如果我想使用存储在数组中的数字,我该怎么办?例如:$ random = rand(1,8)改为$ random = array(1,2,3,...,25)?? – bob 2013-05-07 03:51:32

+0

要直接在数组中选择一个值,您可以执行'$ random [$ i]',其中'$ i'介于'0'和'count($ random)'之间。如果你想随机选择你可以使用['array_rand()'](http://php.net/manual/en/function.array-rand.php) - 'array_rand($ random)'' – Sean 2013-05-07 04:02:16

1

你可以做这样的事情

$sum = 0; 
for($i=0; $i<count($your_array); $i++) 
{ 
    //your codes 
    $sum += $values; //values to be summed up 
} 

echo "Total = ". $sum; 

我希望这有助于。

相关问题