简体   繁体   English

PHP:来自多维数组的HTML表

[英]PHP: HTML table from Multidimensional array

I'm trying to create HTML table from multidimensional php array, however it is partially designed. 我正在尝试从多维php数组创建HTML表,但是它是部分设计的。 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: 我的HTML表代码如下所示:

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 输出应如所附图片所示 在此处输入图片说明

        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? 那这个呢?

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... 希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM