来自爆炸数组的PHP表

时间:2012-03-14 22:20:04

标签: php arrays sorting html-table explode

我有这样的数据:

something something_description, something2 something2_description, something3 something3_description...

现在我需要用PHP来获取表格:

<tr><td>something</td><td>something_description</td></tr>
<tr><td>something2</td><td>something2_decription</td></tr>

我不知道会有多少“某事”和“something_decriptions”,所以我需要设置一些循环。

现在我有了这段代码:

$data = explode(',',$query);
从那里我将获得如下数组:

[0] => something something_description

现在我怎么把它放到桌子里?

在网上我发现了一些将数组排序到表格的例子,但这又是“爆炸”中的一个“爆炸”

我可以使用一些帮助。

2 个答案:

答案 0 :(得分:2)

可能你正在寻找这个:

    $data = explode(',',$query);

    echo '<table>';
    foreach($data as $row){
        echo '<tr>';
        $row = explode(' ',$row);
        foreach($row as $cell){
            echo '<td>';
            echo $cell;
            echo '</td>';
        }
        echo '</tr>';
    }
    echo '</table>';

答案 1 :(得分:0)

试试这个:

echo "<table><tr>".implode("</tr><tr>",array_map(function($a) {return "<td>".implode("</td><td>",explode(" ",trim($a)))."</td>";},explode(",",$query)))."</tr></table>";

单线ftw:p