0

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 picenter image description here

1
  • can someone please take a look on it Commented Jan 7, 2016 at 11:19

2 Answers 2

1
        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?

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

1 Comment

@Tsah, please combine this answer with the one above, people will downvote you if they see this.
0

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...

5 Comments

Although it is better than before, but it is creating another table inside the table. How can that be removed? any idea
You need to create a table within the cell to display the values. Otherwise you can't display two values of an array side by side.
On a second thought, is no of items in array fixed?
The structure of the table would always be same, but the values in the cell (2,2) and onwards could be multiple
also, can you help me out with the expected values.. I tried exploding the values, but its not going to relevant columns. Appreciate your help. HTML issue is fixed. thanks for taht.

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.