0

this is my Multidimensional Array im tring to make a 3x5 table, iknow i have to use foreach, but i don't understand where to go from here, anyone got any suggestions?

<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>
1

4 Answers 4

2

Try this

<?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>";
?>
Sign up to request clarification or add additional context in comments.

Comments

0

you have to nest your foreaches one in an other, like so:

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

EDIT: for the second loop you could uye $key => $value if you need to work with the words "firma", "saleskap" and "siste".

like so: foreach ($value as $key => $value2)

Comments

0

hey @Torstein Søreide understand below way to make your 3*5 table use a loop may be while or foreach you want to insert the values of your array in table like below:

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

all the best

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.