-4

I am trying to output an array as an html table, a sample of the array is..

Array
(
    [18] => Array
        (
            [colour] => red
            [size] => large
            [age] => 220
        )
        [19] => Array
        (
            [colour] => yellow
            [size] => small
            [age] => 20
        )
        [12] => Array
        (
            [colour] => brown
            [size] => large
            [age] => 2
        )
)

I am ok with doing a foreach for a simple array but does anyone have an example showing this being converted into a table for this type of array?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
  • 3
    A table? Like any table? You don't have any requirements more specific than that? Here you go: `
    I'm a table!
    `. (Do at least make an effort to solve this problem, it helps clarify your intent and shows you're committed to solving it.)
    – tadman Aug 21 '17 at 15:26
  • Use multiple `foreach`s? – Jonnix Aug 21 '17 at 15:26
  • Good point, post updated! – fightstarr20 Aug 21 '17 at 15:26
  • 1
    @tadman you know your HTML quite well ;-) basic 101 stuff. – Funk Forty Niner Aug 21 '17 at 15:27
  • 2
    @Fred-ii- Experts Exchange Level A+++ Certified HTML Ph.D! (Fought the urge to add ``) – tadman Aug 21 '17 at 15:27
  • 1
    Possible duplicate of [How to create a HTML Table from a PHP array?](https://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-a-php-array) - Googling `PHP array to table` yielded me about [3,450,000 results](https://www.google.ca/search?q=php+array+to+table) – ctwheels Aug 21 '17 at 15:29
  • 1
    `"I am ok with doing a foreach"` - Then have you tried? `"being converted into a table for this type of array"` - And what 'type of array' is this? Is there some reason a foreach wouldn't iterate over this array? I guess it's not really clear to me where you're actually stuck here. – David Aug 21 '17 at 15:30

1 Answers1

4

Try something like this:

$data = //your array
$html = "<table>";
foreach($data as $row) {
    $html .= "<tr>";
    foreach ($row as $cell) {
        $html .= "<td>" . $cell . "</td>";
    }
    $html .= "</tr>";
}
$html .= "</table>";

Your question is extremenly vague so this is a vague answer, but you should get the general idea :P