我该如何为这个数组创建一个表?

时间:2017-02-13 12:14:26

标签: php arrays foreach

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

4 个答案:

答案 0 :(得分: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>";
?>

答案 1 :(得分:0)

你必须将你的foreaches嵌套在另一个中,如下所示:

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

修改 对于第二个循环你可以uye $ key =&gt; $ value如果你需要使用&#34; firma&#34;,&#34; saleskap&#34;和&#34; siste&#34;。

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

答案 2 :(得分:0)

  嘿@TorsteinSø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> 
  

一切顺利

答案 3 :(得分:0)

&#13;
&#13;
    <?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>";
    ?>              
&#13;
&#13;
&#13;