PHP以列表模式而不是另一个单元格显示值

时间:2016-01-08 02:24:21

标签: php html

我非常接近预期的输出,但是我遇到了困难,需要就以下问题提供一些指导:

我需要将值分组到一个类别中,因此需要在一个单元格中显示。目前,正在创建一个新列。例如,在目标 - &gt;潜在客户生成下,我希望只创建两列,一列具有值 - Male (6.47% cheaper ), Male (15.91% cheaper )以列表模式显示,其他列应具有值5-54 (6.47% cheaper ),35-44 (15.91% cheaper ),再次以列表模式显示。目前,for循环为每个值创建一个特定列。附上的是预期产量。我也使用explode函数来创建数组,但不确定如何创建列表值,因为它可能会弄乱HTML的<td>

生成的PHP数组是这样的:

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

PHP功能:

  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) {
                        $values = explode("^", $value3);
                        $value3 = $values[0];
                        $value4 = $values[1];
                        echo "<td>$value3</td>\n";
                        echo "<td>$value4</td>\n";
                    }
                    echo "</tr></table></td>\n";
                }
                else {

                    echo "<td>$value2</td>\n";
                }

            }
        }
        else {
            echo "<td>$value</td>\n";    
        }

        echo "</tr>";

    }
    echo '</tbody></table>';
    }

电流输出 enter image description here

预期输出:ex彩色单元格有多个值,我想每次在一个单元格中显示它们而不是不同的列 enter image description here

1 个答案:

答案 0 :(得分:0)

您想要做的就是替换

echo "<td>$value3</td>\n";
echo "<td>$value4</td>\n";

echo "<td>$value3<br>$value4</td>\n";