PHP two-dimensional array in HTML

A simple Google search will reveal many solutions for converting a two-dimensional array into HTML using PHP. Unfortunately, none of them have the answers I'm looking for.

I need a generic piece of code that converts an array to an HTML table. Where most examples go wrong is that they assume that the programmer knows the name of the fields in the table. I want this code to be generic so that I can use it even if I don't know the field names.

I see, I need two cycles. One nested inside the other. I am not sure how to get the values ​​because I do not know the keys.

The final result, we hope, will 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 that I need a general and simple solution. Hope this is clear.

+6
source share
3 answers

The following code will look at two dimensions of the array and turn them into a table. No matter what the key may be, you get a visual representation of this. If they do have a key name, not just an index, the values ​​will be available in $key and $subkey respectively. Therefore, 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; 

Result:

enter image description here

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

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

As a result:

enter image description here

+13
source

You will need two loops. One to go through the first level, and the other to go through the second level. This assumes 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>'; 

Not tested, but should do it for you. The first level of the array is the rows, the second level of the array is the columns.

Sample output for our $ foo array:

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

Your loops will be something like this:

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

In $ k you save the key, in $ v the value ... Then you can print both.

-1
source

Source: https://habr.com/ru/post/915604/


All Articles