6

A simple Google search will reveal a multitude of solutions for converting a two dimension array into HTML using PHP. Unfortunately none of these has the answers I am looking for.

I want a generic piece of code that converts an array into a HTML table. Where most examples go wrong is that they assume the programmer knows the name of the table's fields . I want this code to be generic such that I can use it even if I do not know the name of the fields.

I can see I need two loops. One nested inside of the other. What I am not sure of is how to get the values out given I don't know the keys.

The end result will hopefully output html something like this:

<th>
  <td>x/y</td>
  <td> x1 </td>
  <td> x2 </td>
</th>
<tr>
  <td>y1</td>
  <td> x1y1 </td>
  <td> x2y1 </td>
</tr>
<tr>
  <td>y2</td>
  <td> x1y2 </td>
  <td> x2y2 </td>
</tr>

Please remember I want a generic and simple solution. I hope this is clear.

0

3 Answers 3

14

The following code will look through the two dimensions of the array and make them into a table. Regardless of what the key may be, you will get a visual representation of it. If they do have key name and not just an index, the values will be available in $key and $subkey respectively. So you have them if you need them.

The code:

$myarray = array("key1"=>array(1,2,3,4),
                 "key2"=>array(2,3,4,5),
                 "key3"=>array(3,4,5,6),
                 "key4"=>array(4,5,6,7)); //Having a key or not doesn't break it
$out  = "";
$out .= "<table>";
foreach($myarray as $key => $element){
    $out .= "<tr>";
    foreach($element as $subkey => $subelement){
        $out .= "<td>$subelement</td>";
    }
    $out .= "</tr>";
}
$out .= "</table>";

echo $out;

The result:

enter image description here

If you want to see the keys as headings, you could add this code after the echo "<table>"; line:

echo "<tr>";
foreach($myarray as $key => $element) echo "<td>$key</td>";
echo "</tr>";

Resulting in this:

enter image description here

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

6 Comments

Thanks @thisMayhem your answer is correct. I have simplified it as I could not see the point of the key. I will put it below.
I just added it there, in case you needed it as I said. I'm glad my answer was helpful :) please consider upvoting and marking it as valid answer, @Brett
thanks and done @thisMayhem. I have been told I can't answer my own question for seven hours so you's have to wait.
I think your header row is sideways. You just can't tell here because you've used diagnoally symmetrical data. If you were to use input 1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16 then the headings wouldn't lign up properly here.
Jake M, I fail to see your point. Tried with you data and the headings align perfectly fine
|
3

You'll need two loops. One to loop through the first level, and another to go through the second level. This assumes that your two dimensional array is regularly rectangular.

//our example array
$foo['one']['a'] = '1a';
$foo['one']['b'] = '1b';
$foo['two']['a'] = '2a';
$foo['two']['b'] = '1b';

//open table
echo '<table>';

//our control variable
$first = true;

foreach($foo as $key1 => $val1) {
    //if first time through, we need a header row
    if($first){
        echo '<tr><th></th>';
        foreach($val1 as $key2 => $value2){
            echo '<th>'.$key2.'</th>';
        }
        echo '</tr>';

        //set control to false
        $first = false;
    }

    //echo out each object in the table
    echo '<tr><td>'.$key1.'</td>';
    foreach($val1 as $key2 => $value2){
        echo '<td>'.$value2.'</td>';
    }
    echo '</tr>';
}

echo '</table>';

Haven't tested it, but that should do it for you. First level of the array is rows, second level of the array is columns.

Sample Output for our $foo array:

+-----+-----+-----+
|     |  a  |  b  |
+-----+-----+-----+
| one | 1a  | 1b  |
+-----+-----+-----+
| two | 2a  | 2b  |
+-----+-----+-----+

2 Comments

I do mean if($first), thanks. As for $foo, it was just an example array. I'm assuming he already has properly formatted input, I just put it there as an example of the fact that it needs to be rectangular input for the header rows to work properly. As for echoing vs rendering outside of php tags, I used echo for simplicity here. If I were using this, I'd wrap it in a function, put all the html into an output variable instead of echoing, and return it to be echoed later.
I gave you a +1, Jake. You put effort into this. What's with people?
-1

Your loops will be something like this:

foreach($myArray as $k => $v)

In $k you'll kave the key, in $v the value... Then you can print both.

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.