I need to convert an HTML table (rows and columns) to PHP array. for example:
<tr>
<th rowspan="2">Username</th>
<th colspan="2">Personal</th>
</tr>
<tr>
<th>Name</th>
<th>Birth date</th>
</tr>
in PHP I need it to become:
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"
)
)
);
so, the idea is that, the first array will keep the rows, inside each row I want to have an array of columns and inside of columns I want an array with the value of the cell and the atributes, I just need attributes like rowspan and colspan. so if you got the idea and know how to do it please share, I dont need you to do it for me, I just need to know how can I do this. Thanks
SOLUTION( 1st Mar 2013):
So, here is the solution:
first I send a form with a field containing the html table tags.
and then I get the string containig the html, I am doing it with symfony, so in my action I wrote:
$stringHeader = $request->getParameter('htmlTable');
htmlTable is the name of the field (input) from the form containing the html table
then I convert the string to xml doing:
$xmlstring = <<<XML
<?xml version='1.0'?>
<document>
$stringHeader
</document>
XML;
and finally put the data to the array, the structure of the html is as mentioned.
$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();
}