Question : Multidimension Associative Array to HTML table

I'm trying to create a function that outputs a 2 dimensional associative array to an HTML table in the format below. I've been trying to adjust a multidimension array to table function I found online to work as I need it to but I've not been able to.

HTML Output required
<table>
  <tr>
    <th>Code</th>
    <th>Description</th>
    <th>Price</th>
  </tr>
  <tr>
    <td>TIR</td>
    <td>Tires</td>
    <td>100</td>
  </tr>
  <tr>
    <td>OIL</td>
    <td>Oil</td>
    <td>10</td>
  </tr>
  <tr>
    <td>SPK</td>
    <td>Spark Plugs</td>
    <td>4</td>
  </tr>
</table>
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
$arr_products = array( array( 'Code' => 'TIR', 'Description' => 'Tires', 'Price' => 100 ), array( 'Code' => 'OIL', 'Description' => 'Oil', 'Price' => 10 ), array( 'Code' => 'SPK', 'Description' => 'Spark Plugs', 'Price' =>4 ) ); 

function show_array2($array, $level, $sub){
	if (is_array($array) == 1){          // check if input is an array
	   foreach($array as $key_val => $value) {
		   if (is_array($value) == 1){   // array is multidimensional
			   echo "<tr>";
			   show_array2($value, $level+1, 1);
		   }
		   else{                        // (sub)array is not multidim
			   if ($sub != 1){          // first entry for subarray
				   echo "<tr nosub>";
			   }
			   $sub = 0;
			   echo "<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_array2($array){
	echo "<table cellspacing=\"0\" border=\"2\">\n";
	show_array2($array, 1, 0);
	echo "</table>\n";
}
	
html_show_array2($arr_products);

Answer : Multidimension Associative Array to HTML table

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
<?php // RAY_temp_benners.php
error_reporting(E_ALL);
$arr_products = array( array( 'Code' => 'TIR', 'Description' => 'Tires', 'Price' => 100 ), array( 'Code' => 'OIL', 'Description' => 'Oil', 'Price' => 10 ), array( 'Code' => 'SPK', 'Description' => 'Spark Plugs', 'Price' =>4 ) ); 

// ACTIVATE THIS TO LOOK AT THE ARRAY
// var_dump($arr_products);

function RAY_show_array($a)
{
    // CREATE THE TABLE HEADERS
    echo "<tr>";
    // GET KEYS FROM ONE OF THE SUB-ARRAYS
    $h = $a[0];
    // WRITE TABLE HEADERS FROM THE KEYS
    foreach ($h as $k => $x)
    {
        echo "<th>" . $k . "</th>";
    }	
    echo "</tr>" . PHP_EOL;
    
    // CREATE THE TABLE DATA FROM THE SUB-ARRAYS
    foreach ($a as $s)
    {
        echo "<tr>";
        // GET DATA FROM EACH SUB-ARRAY
        foreach ($s as $x)
        {
            // WRITE THE DATA ELEMENTS INTO TD STATEMENTS
            echo "<td>" . $x . "</td>";
        }
        echo "</tr>" . PHP_EOL;
    }
}

function html_show_array2($array){
	echo "<table cellspacing=\"0\" border=\"2\">\n";
	RAY_show_array($array, 1, 0);
	echo "</table>\n";
}
	
html_show_array2($arr_products);
Random Solutions  
 
programming4us programming4us