Reputation: 6334
I'm trying to create HTML table from multidimensional php array, however it is partially designed. Can some one please take a loom and advise.
This is how my Multidimensional array looks like:
array(2) { [0]=> array(3)
{ ["Objective"]=> string(11) "Conversions"
["Top Performing Targeting Group"]=> array(2) { [0]=> string(48) "Female (27.74% cheaper )^25-34 (27.74% cheaper )" [1]=> string(48) "Female (22.52% cheaper )^18-24 (22.52% cheaper )" }
["Top Performing Placement"]=> array(1) { [0]=> string(52) "Mobile Feed (13.53% cheaper)^iPhone (13.53% cheaper)" } }
[1]=> array(3)
{ ["Objective"]=> string(10) "Page Likes"
["Top Performing Targeting Group"]=> array(0) { }
["Top Performing Placement"]=> array(2) { [0]=> string(50) "Mobile Feed (1.42% cheaper)^iPhone (1.42% cheaper)" [1]=> string(51) "Mobile Feed (1.71% cheaper)^Android (1.71% cheaper)" } } }
My HTML table code looks like:
function generateTable2($associative_array){
echo '<table width="620" class="optimization_table"><thead><tr><th>';
echo implode('</th><th>', array_keys(current($associative_array)));
echo '</th></tr></thead><tbody>';
foreach ($associative_array as $row=>$value){
echo "<tr><td>";
if(is_array($value)) {
foreach ($value as $k => $v) {
if(is_array($v))
{
foreach ($v as $key => $value) {
//explode("^",$v)
echo "</td>";
print_r($key);
print_r($value);
//explode("^",$k)[2]
# code...
}
}
//echo implode('</td><td>', $v);
else echo "$v"."</td><td>";
}
}
//echo implode('</td><td>', $value);
//else echo implode('</td><td>', $value);
else echo "$value"."</td><td>";
}
echo '</tbody></table>';
}
Output should look like in the attached pic
Upvotes: 0
Views: 125
Reputation: 2768
function generateTable2($associative_array){
echo '<table width="620" class="optimization_table" border="1" cellspacing="0" cellpadding="0"><thead><tr><th colspan=2>';
echo implode('</th><th colspan=2>', array_keys(current($associative_array)));
echo '</th></tr></thead><tbody>';
foreach ($associative_array as $row=>$value){
echo "<tr>";
if(is_array($value)) {
foreach($value as $value2) {
if(is_array($value2)) {
foreach($value2 as $value3) {
echo "<td>$value3</td>\n";
}
}
else {
echo "<td colspan=2>$value2</td>\n";
}
}
}
else {
echo "<td colspan=2>$value</td>\n";
}
echo "</tr>";
}
echo '</tbody></table>';
}
what about this?
Upvotes: 1
Reputation: 2768
A not so sophisticated solution...
function generateTable2($associative_array){
echo '<table width="620" class="optimization_table" border="1" cellspacing="0" cellpadding="0"><thead><tr><th>';
echo implode('</th><th>', array_keys(current($associative_array)));
echo '</th></tr></thead><tbody>';
foreach ($associative_array as $row=>$value){
echo "<tr>";
if(is_array($value)) {
foreach($value as $value2) {
if(is_array($value2)) {
echo "<td><table border='1' cellspacing='0' cellpadding='0'><tr>";
foreach($value2 as $value3) {
echo "<td>$value3</td>\n";
}
echo "</tr></table></td>\n";
}
else {
echo "<td>$value2</td>\n";
}
}
}
else {
echo "<td>$value</td>\n";
}
echo "</tr>";
}
echo '</tbody></table>';
}
Hope this helps...
Upvotes: 0