PHP:来自多维数组的HTML表

时间:2016-01-07 10:40:03

标签: php html

我试图从多维php数组创建HTML表,但它是部分设计的。有人可以拿一台织机并提出建议。

这就是我的多维数组的样子:

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)" } } }

我的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>';
}

输出应如附图[{3}}

中所示

2 个答案:

答案 0 :(得分:1)

<defs>
  <g id="cursor-symbol">
    <circle fill-opacity="0.8" fill="white" r="10.0" cx="10.0" cy="10.0" stroke="black" stroke-width="2.0" stroke-opacity="1"/>
    <line y2="20.0" x1="10.0" x2="0.0" transform="rotate(45.0, 10.0, 10.0)" y1="10.0"/>
  </g>
  <cursor id="cursor" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#cursor-symbol"/>
</defs>
...
<rect cursor="url(#cursor)" x="0" y="0" width="100" height="100" />

这是怎么回事?

答案 1 :(得分:0)

一个不太复杂的解决方案......

        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>';
        }

希望这会有所帮助......