how can i create a table for this array?

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>

Upvotes: 0

Views: 73

Answers (4)

doublethug
doublethug

Reputation: 162

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

Upvotes: 0

lazyCoder
lazyCoder

Reputation: 2561

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

Upvotes: 0

Klaus Mikaelson
Klaus Mikaelson

Reputation: 572

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

Upvotes: 2

Iamnino
Iamnino

Reputation: 380

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)

Upvotes: 0

Related Questions