2017-02-13 117 views
0

这是我的多维数组即将做出3x5表,我知道我必须使用foreach,但我不明白从哪里去,这里有人有什么建议吗?我怎样才能为这个数组创建一个表?

<table> 
    <?php 
    $something = array( 
         array("firma" => "ASG", 
          "selskap" => "ABG Sundal Collier", 
          "siste" => 5.95 
         ), 
        array("firma" => "AFG", 
          "selskap" => "AF Gruppen", 
          "siste" => 122 
         ), 
        array("firma" => "AKVA", 
          "selskap" => "AKVA Group ", 
          "siste" => 47.2 
         ), 
        array("firma" => "AGA", 
          "selskap" => "Agasti Holding", 
          "siste" => 1.2 
         ), 
        array("firma" => "AKA", 
          "selskap" => "Akastor", 
          "siste" => 6.04 
         ), 

        );           
    ?>   
    </table> 
+0

重复:http://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-一个-PHP-阵列 – JustBaron

回答

2

试试这个

<?php 
     $something = array( 
         array("firma" => "ASG", 
          "selskap" => "ABG Sundal Collier", 
          "siste" => 5.95 
         ), 
        array("firma" => "AFG", 
          "selskap" => "AF Gruppen", 
          "siste" => 122 
         ), 
        array("firma" => "AKVA", 
          "selskap" => "AKVA Group ", 
          "siste" => 47.2 
         ), 
        array("firma" => "AGA", 
          "selskap" => "Agasti Holding", 
          "siste" => 1.2 
         ), 
        array("firma" => "AKA", 
          "selskap" => "Akastor", 
          "siste" => 6.04 
         ), 

        ); 
echo "<table border='2'>"; 
echo " <tr> 
       <td>firma</td> 
       <td>selskap</td> 
       <td>siste</td> 
      </tr>"; 
foreach ($something as $thing){ 
    echo " <tr> 
       <td>".$thing['firma']."</td> 
       <td>".$thing['selskap']."</td> 
       <td>".$thing['siste']."</td> 
      </tr> 
    "; 
} 
echo "</table>"; 
?> 
0

你必须巢您foreaches一个在其他的,像这样:

foreach ($something as $value){ 
    foreach ($value as $value2){ 
     // do what you want with $value and $value2 
    } 
} 

编辑: 你可以,如果你需要初级会员$关键=> $值的第二循环与“firma”,“saleskap”和“siste”一起工作。

像这样:foreach ($value as $key => $value2)

0

哎@TorsteinSøreide理解下面的方法来让你的3 * 5台使用 循环可能是同时或的foreach要插入您的 阵列的值如下表所示:

<?php 
     $something = array( 
          array("firma" => "ASG", 
           "selskap" => "ABG Sundal Collier", 
           "siste" => 5.95 
          ), 
          array("firma" => "AFG", 
            "selskap" => "AF Gruppen", 
            "siste" => 122 
           ), 
          array("firma" => "AKVA", 
            "selskap" => "AKVA Group ", 
            "siste" => 47.2 
           ), 
          array("firma" => "AGA", 
            "selskap" => "Agasti Holding", 
            "siste" => 1.2 
           ), 
          array("firma" => "AKA", 
            "selskap" => "Akastor", 
            "siste" => 6.04 
           ), 

        ); 

    ?> 
    <table border = 1 align = "center"> 
     <tr> 
      <th>firma</th> 
      <th>selskap</th> 
      <th>siste</th> 
     </tr> 
     <?php 
     foreach ($something as $value) { 
      ?> 
      <tr> 
       <td><?php echo $value["firma"] ?></td> 
       <td><?php echo $value["selskap"] ?></td> 
       <td><?php echo $value["siste"] ?></td> 

      </tr> 
      <?php 

     } 
     ?> 


    </table> 

所有最好的

0

<?php 
 
    $something = array( 
 
         array("firma" => "ASG", 
 
          "selskap" => "ABG Sundal Collier", 
 
          "siste" => 5.95 
 
         ), 
 
        array("firma" => "AFG", 
 
          "selskap" => "AF Gruppen", 
 
          "siste" => 122 
 
         ), 
 
        array("firma" => "AKVA", 
 
          "selskap" => "AKVA Group ", 
 
          "siste" => 47.2 
 
         ), 
 
        array("firma" => "AGA", 
 
          "selskap" => "Agasti Holding", 
 
          "siste" => 1.2 
 
         ), 
 
        array("firma" => "AKA", 
 
          "selskap" => "Akastor", 
 
          "siste" => 6.04 
 
         ), 
 

 
        ); 
 
echo "<table>"; 
 
foreach ($something as $count => $arrValue){ 
 
    // this will output header 
 
    if (!count){ 
 
     echo "<th>"; 
 
     foreach ($arrValue as $key => $text){ 
 
      echo "<td>".$text."</td>" 
 
     } 
 
     echo "<th>"; 
 
    } 
 
    //this will output the body 
 
    echo "<tr>"; 
 
    foreach ($arrValue as $key => $text){ 
 
     echo "<td>".$text."</td>" 
 
    } 
 
    echo "<tr>"; 
 
} 
 
echo "</table>"; 
 
    ?>