Page 1 of 1

How do you output a Multi Dimensional Array to a Table

Posted: Wed Feb 08, 2006 5:20 am
by Benjamin
I am stumped on this one.

Here is an example of the array structure:
Array
(
[0] => Array
(
[One] => 1
[Two] => 6
[Three] => 4
[Four] => 38
)

[1] => Array
(
[One] => 16
[Two] => 2
[Three] => 0
[Four] => 156
)

[2] => Array
(
[One] => 4
[Two] => 1
[Three] => 0
[Four] => 19522
)

[3] => Array
(
[One] => 5
[Two] => 1
[Three] => 1
[Four] => 0
)

)
And here is the code I wrote which does not work because the $Field value is always "Array":

Code: Select all

foreach ($Array as $Field) {
  foreach ($Field as $Data) {
    echo "<b>" . current($Field) . "</b><br />";
    switch ($Field) {
      case "One":
        $Name = get_name($Data);
        $Table .= "          <tr>\n";
        $Table .= "            <td>$Name</td>\n";
        break;
      case "Two":
        $Table .= "            <td>$Data</td>\n";
        break;
      case "Three":
        $Table .= "            <td>$Data</td>\n";
        break;
      case "Four":
        $Table .= "            <td>$Data</td>\n";
        $Table .= "            <td>&nbsp;</td>\n";
        $Table .= "          </tr>";
        break;
    }
  }
}

Posted: Wed Feb 08, 2006 5:27 am
by Weirdan
shouldn't it be something like this ?:

Code: Select all

foreach ($Array as $outerKey => $Field) {
  foreach ($Field as $innerKey => $Data) {
    echo "<b>" . $outerKey . "</b><br />";
    switch ($outerKey) {
      case 1:
        $Name = get_name($Data);
        $Table .= "          <tr>\n";
        $Table .= "            <td>$Name</td>\n";
        break;
      case 2:
        $Table .= "            <td>$Data</td>\n";
        break;
      case 3:
        $Table .= "            <td>$Data</td>\n";
        break;
      case 4:
        $Table .= "            <td>$Data</td>\n";
        $Table .= "            <td>&nbsp;</td>\n";
        $Table .= "          </tr>";
        break;
    }
  }
}

Posted: Wed Feb 08, 2006 5:32 am
by Benjamin
Well that is closer as case 1 did fire but it still didn't really work. Here is the output:
<tr>

<td>Name 1</td>
<tr>
<td>Name 2</td>
<tr>
<td></td>
<tr>
<td></td>
<td>4</td>

<td>1</td>
<td>0</td>
<td>19522</td>
<td>5</td>
<td>1</td>
<td>1</td>

<td>0</td>

Posted: Wed Feb 08, 2006 6:09 am
by Benjamin
Well I don't know if this is the right way to do it or not but it works.

Code: Select all

foreach ($Array as $Field) {
echo "Field 1 " . current($Field) . "<br>";
echo "Field 2 " . next($Field) . "<br>";
echo "Field 3 " . next($Field) . "<br>";
echo "Field 4 " . next($Field) . "<br>";
}

Posted: Wed Feb 08, 2006 6:13 am
by m3mn0n
Don't do a foreach directly inside another foreach... simply use keys for the $Field variable. To get a better understanding of what I mean, run this:

Code: Select all

<?php
foreach ($Array as $Field) {
      echo "<pre>";
        print_r ($Field);
      echo "</pre>";
    echo "<br /><br />";
} 
?>

Posted: Wed Feb 08, 2006 8:58 am
by feyd
I prefer doing this kinda stuff like so:

Code: Select all

foreach($Array as $record) {
  echo '<tr><td>' . implode('</td><td>' . $record) . '</td><tr>' . "\n";
}

Posted: Wed Feb 08, 2006 3:05 pm
by raghavan20
feyd wrote:I prefer doing this kinda stuff like so:

Code: Select all

foreach($Array as $record) {
  echo '<tr><td>' . implode('</td><td>' . $record) . '</td><tr>' . "\n";
}
what is so interesting point here is...the style in which he wanted to display is not mentioned unless i terribly read the post...if you want to display in tables as just one row for every single array contained...

I have considered arrays to be of different sizes but this code should work in any case...

Code: Select all

echo "<table>";
foreach($tempArray as $anotherArray){
	echo "<tr>";
	for ($i = 0; $i < count($anotherArray); $i++){ //considering that there are variable number of elements in each array
		echo "<td>".$anotherArray[$i]."</td>";
	}
	echo "</tr>";	
}
echo "</table>";