【问题标题】:how can i create a table for this array?我怎样才能为这个数组创建一个表?
【发布时间】:2017-07-01 10:09:27
【问题描述】:

这是我的多维数组,我想制作一个 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>

【问题讨论】:

标签: php arrays foreach


【解决方案1】:

你必须将你的 foreaches 一个一个地嵌套在一起,像这样:

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

编辑: 对于第二个循环,如果您需要使用“firma”、“saleskap”和“siste”这些词,您可以使用 $key => $value。

像这样:foreach ($value as $key =&gt; $value2)

【讨论】:

    【解决方案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>";
    ?>
    

    【讨论】:

      【解决方案3】:

      嘿@Torstein Søreide 了解以下让您的 3*5 桌子使用 循环可能是 while 或 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> 
      

      一切顺利

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2011-04-27
          • 2017-08-11
          • 2017-07-06
          • 2022-10-24
          • 2021-10-30
          • 2022-12-10
          • 2021-07-10
          • 2021-12-29
          • 1970-01-01
          相关资源
          最近更新 更多