PHP table from explode array

妖精的绣舞 提交于 2019-12-29 09:39:10

问题


I have data like this:

something something_description, something2 something2_description, something3 something3_description...

And now I need with PHP to get table as:

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

I don't know how many "something" and "something_decriptions" will it be so I need to set some loop.

for now I have this code:

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

from that I will get array like:

[0] => something something_description

Now how can I put this into table?

On net I found some examples for sorting array to table, but this is with one more "explode" inside "explode"

I could use some help.


回答1:


Probably you are looking for this:

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



回答2:


Try this:

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

One-liner ftw :p



来源:https://stackoverflow.com/questions/9711175/php-table-from-explode-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!