You want to print your multidimensional array into a html table.
All you have to do is to save the first part of the following code
to a file called "demo.php".
<html> <body>
<?php include("show_array.php");
$array['id'] = 5; $array['text'] = "Mein Text"; $array['comments'] = array(5,4,6,23); $array['author'] = "ich";
html_show_array($array);
?> <?php include_once("analyticstracking.php") ?> </body> </html>
Now save the following part to "show_array.php" and put both on
your webserver. Show the result by calling demo.php. Or just try
it out here.
<?php function do_offset($level){ $offset = ""; // offset for subarry for ($i=1; $i<$level;$i++){ $offset = $offset . "<td></td>"; } return $offset; }
function show_array($array, $level, $sub){ if (is_array($array) == 1){ // check if input is an array foreach($array as $key_val => $value) { $offset = ""; if (is_array($value) == 1){ // array is multidimensional echo "<tr>"; $offset = do_offset($level); echo $offset . "<td>" . $key_val . "</td>"; show_array($value, $level+1, 1); } else{ // (sub)array is not multidim if ($sub != 1){ // first entry for subarray echo "<tr nosub>"; $offset = do_offset($level); } $sub = 0; echo $offset . "<td main ".$sub." width=\"120\">" . $key_val . "</td><td width=\"120\">" . $value . "</td>"; echo "</tr>\n"; } } //foreach $array } else{ // argument $array is not an array return; } }
function html_show_array($array){ echo "<table cellspacing=\"0\" border=\"2\">\n"; show_array($array, 1, 0); echo "</table>\n"; } ?>
|