0

i want to create a table using an associative key value pair array. i want its keys as column heading of the table and the values should be in corresponding rows cells. array may look like it

 Array
(
    [0] => Array
        (
            [field1] => 18270
            [field2] => 24
            [field3] => 7830
            [field4] => 44
            [field5] => 5
            [field6] => 15660
            [field7] => 77
            [field8] => 0
            [field9] => 0
            [field10] => 0
        )

    [1] => Array
        (
            [field1] => 35070
            [field2] => 24
            [field3] => 15030
            [field4] => 44
            [field5] => 5
            [field6] => 30060
            [field7] => 77
            [field8] => 0
            [field9] => 0
            [field10] => 0
        )

    [2] => Array
        (
            [field1] => 16051
            [field2] => 24
            [field3] => 6879
            [field4] => 44
            [field5] => 5
            [field6] => 13758
            [field7] => 77
            [field8] => 0
            [field9] => 0
            [field10] => 0
        )

    [3] => Array
        (
            [field1] => 15050
            [field2] => 24
            [field3] => 6450
            [field4] => 44
            [field5] => 5
            [field6] => 12900
            [field7] => 77
            [field8] => 0
            [field9] => 0
            [field10] => 0
        )

    [4] => Array
        (
            [field1] => 15750
            [field2] => 24
            [field3] => 6750
            [field4] => 44
            [field5] => 5
            [field6] => 13500
            [field7] => 77
            [field8] => 0
            [field9] => 0
            [field10] => 0
        )

    [5] => Array
        (
            [field1] => 15750
            [field2] => 24
            [field3] => 6750
            [field4] => 44
            [field5] => 5
            [field6] => 13500
            [field7] => 77
            [field8] => 0
            [field9] => 0
            [field10] => 0
        )

    [6] => Array
        (
            [field1] => 15050
            [field2] => 24
            [field3] => 6450
            [field4] => 44
            [field5] => 5
            [field6] => 12900
            [field7] => 77
            [field8] => 0
            [field9] => 0
            [field10] => 0
        )
)

and table should look like this table_image i am having problem in creating thead dynamically and putting corresponding values in the columns below it

1
  • What you have tried so far. Post your attempts. Commented Feb 17, 2016 at 11:14

2 Answers 2

2

Try this:

<?php 
foreach ($table as $rowIndex=>$row) {
  if($rowIndex == 0) {
     echo "<thead><tr>";
     foreach ($row as $columnName=>$cell) {
        if(strpos($columnName, "field") === true) {
           echo "<td>$columnName</td>";
        }
     }
     echo "</tr></thead>";
    }
    echo "<tr>";
    foreach ($row as $columnName=>$cell) {
        if(strpos($columnName, "field") === true) {
           echo "<td>$cell</td>";
        }
    }
    echo "</tr>";
}
?>

more about strpos

Sign up to request clarification or add additional context in comments.

10 Comments

well it does help.. but i have other key value pairs in that array out of which i want to show just these 10. do u know how do i exclude them.
in main array or with field1 field2 etc?? just give an example so that we can figure it out. There would be some combination present on which we can filter those keys
not in main array.. with field1 field2... ex: Array ( [0] => Array ( [id]=>45 [name]=>'abc' [field1] => 18270 [field2] => 24 [field3] => 7830 [field4] => 44 [field5] => 5 [field6] => 15660 [field7] => 77 [field8] => 0 [asddfasd] => 3454 [field9] => 0 [field10] => 0 ['end'] => [field11] => null )
On the code above, you could try adding this inside the first foreach: if ($columnName == 'field11') { break; }. However you are not helping yourself by showing partial code and letting us guess.
better to create a temp array of these fields (which you want to use) inside first fooreach loop and then use it in nested foreach
|
1

Though not pretty, the following should do it:

<?php
echo '<table>';
foreach (array_name as $row => $columns) {
    if ($row == 0) { //if it's the first row in our dataset
        //first print the table header
        echo '<thead><tr>';
        foreach ($columns as $header => $value) {
            echo '<th>' . $header . '</th>';
        }
        echo '</tr><thead>';
    }
        //then print the data as usual
        echo '<tr>';
        foreach ($columns as $header => $value) {
            echo '<td>' . $value . '</td>';
        }
        echo '</tr>';
}
echo '</table>';
?>

PS: beat to the punch by kamaldeep-singh-bhatia :)

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.