繁体   English   中英

HTML表格到PHP数组,并带有colspan和rowspan

[英]HTML Table to PHP array with atributes colspan and rowspan

我需要将HTML表(行和列)转换为PHP数组。 例如:

<tr>
   <th rowspan="2">Username</th>
   <th colspan="2">Personal</th>
</tr>
<tr>
    <th>Name</th>
    <th>Birth date</th>
</tr>

在PHP中,我需要它成为:

array(
     [0] => array(
                 [0] => array(
                             ["value"] => "Username", 
                             ["rowspan"] => "2"),
                 [1] => array(
                             ["value"] => "Personal", 
                             ["colspan"] => "2")
                 ),
     [1] => array(
                 [0] => array(
                             ["value"] => "Name"
                             ),
                 [1] => array(
                             ["value"] => "Birth date"
                             )
                 )
);

所以,我的想法是,第一个数组将保留行,在每个行内我想要一个列数组,在列内我想要一个带有单元格值和属性的数组,我只需要像rowpan这样的属性和科尔斯潘。 因此,如果您有主意并知道如何做,请分享,我不需要您为我做,我只需要知道我该怎么做。

你应该使用数组的组合

array(
       array("rowspan"=>3,"colspan=>"2","class"=>"abc",id=>"row_1","value"=>array(1,2)
      );

第一个数组包含用于属性的数组和用于显示数据的值数组

解决方案(2013年3月1日)

因此,这是解决方案:首先,我发送一个表单,该表单的字段包含html表标签。 然后我得到包含html的字符串,我正在用symfony来做,所以在我的动作中,我写道: $stringHeader = $request->getParameter('htmlTable'); htmlTable是包含html表的表单中的字段(输入)的名称,然后将字符串转换为XML:

$xmlstring = <<<XML
<?xml version='1.0'?> 
<document>
$stringHeader
</document>
XML;

最后将数据放入数组,html的结构如前所述。

$xml = simplexml_load_string($xmlstring);
$header = array();
$columns = array();
foreach ($xml->tr as $tr) {
    foreach ($tr->th as $th) {
        if (isset($th->attributes()->rowspan)) {
            $rowspan = $th->attributes()->rowspan;
            $columns[] = array("value" => "$th", "rowspan" => "$rowspan");
        } elseif (isset($th->attributes()->colspan)) {
            $colspan = $th->attributes()->colspan;
            $columns[] = array("value" => "$th", "colspan" => "$colspan");
        } else {
            $columns[] = array("value" => "$th");
        }

    }
    $header[] = $columns;
    unset($columns);
    $columns = array();
}

暂无
暂无

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

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