2015-11-03 105 views
2

這2個陣列的多維度的乘法代碼:乘2陣列的多維度

<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title></title> 
    </head> 
    <body> 
     <?php 
     $numberArray = array(
      array(1, 2, 3, 4, 7, 6), 
      array(2, 3, 1, 0, 5) 
     ); 
     printTable($numberArray); 
     function printTable($numberArray) { 
      // Placeholder 
      $result = []; 

      // Setup the multiplication 
      foreach ($numberArray[1] as $key1 => $value1) { 
       $tmp = array($value1); // add index y-axis 
       foreach ($numberArray[0] as $key0 => $value0) { 
        $tmp[] = $value0 * $value1; 
       } 
       $result[] = $tmp; 
      } 

      // Add index the x-axis 
      array_unshift($result, array_merge(array(" "), $numberArray[0])); 

      // Loop through the $result array and display the table 
      echo "<table border='1'>"; 
      foreach ($result as $key => $value) { 
       echo "<tr>"; 
       foreach ($value as $k => $v) { 
        if ($k == 0 || $key == 0) { 
         echo sprintf("<td><b>%s</b></td>", $v); 
         continue; 
        } 
        echo "<td>$v</td>"; 
       } 
       echo "</tr>"; 
      } 
      echo "</table>"; 
     } 

     ?> 

    </body> 
</html> 
  1. 仍不能很好地運行,有一些代碼仍然不是很確定echo sprintf("<td><b>%s</b></td>", $v);任何人都可以幫忙嗎?
  2. 設置所有乘法和數組也不能顯示結果。
  3. 輸出應該是這樣的例子:here
+0

你有你的答案嗎?如果是,那麼不要修改你的問題,只要選擇正確的答案。 – AkshayP

回答

1

的函數是可以反覆在程序中使用的語句塊。加載頁面時,函數不會立即執行。函數將通過調用該函數來執行。

你必須調用像下面執行代碼的功能,

printTable($numberArray); 

這樣你的代碼如下:

<?php 
    $numberArray = array(
     array(1, 2, 3, 4, 7, 6), 
     array(2, 3, 1, 0, 5) 
    ); 
    printTable($numberArray); //write this function call here for your expected result 
    function printTable($numberArray) { 
     // Placeholder 
     $result = []; 
+0

我明白了你的答案。謝謝@ A.P。 –

+0

如果我想從用戶更改數組如何設置? –

+0

對於這個問題,你可以問另一個問題:) – AkshayP