Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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();
}
share|improve this question
    
what have you tried? –  KaeruCT Feb 26 '13 at 17:01
    
Why are you trying to do something? What you like to do so? –  wumm Feb 26 '13 at 17:03
    
I don't know what you mean by "convert" Are you trying to parse this HTML into PHP? Does the HTML represent data that is POSTed to PHP? –  Mike Brant Feb 26 '13 at 19:05
    
Convert it to json or xml in javascript, and then send it to php. –  pir abdul wakeel Feb 26 '13 at 19:11
    
I developed a method that receives this type of array and then writes the values to an excel file using PHPExcel. The idea is, I have reports generated at runtime, so, the fields are chosen dynamically, and I cant do a static template using PHPExcel, so, I found a solution using arrays, and tested this array as an input of my method and it worked very well, I will improve the method. But now I have to know how to obtain this Html as an array. Pass the html to a php script is not a problem I want the idea to parse this html to an array. –  Paulo Braga Feb 26 '13 at 22:22

1 Answer 1

you should use combination of array like

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

the first array contains array for attriubtes and value array for displaying data

share|improve this answer
    
I am next to the solution, I will share when it be done. –  Paulo Braga Feb 27 '13 at 22:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.