skip to Main Content

I Want to convert an html table to Array using php
I trying to get output like this:

array(
'name' => "test1",
'value' => "test1-1",
),
array(
'name' => "test2",
'value' => "test2-2",
),

my output now:

Array
(
[0] => Array
(
[test1] => test1-1
[test2] => test2-2

Is this possible?

<?php

    $htmlContent = "<table>
  
  <tr>
    <th>test1</th>
    <td>test1-1</td>
  </tr>
  <tr>
    <th>test2</th>
    <td>test2-2</td>
  </tr>
</table>
"
        
    $DOM = new DOMDocument();
    $DOM->loadHTML($htmlContent);
    
    $Header = $DOM->getElementsByTagName('th');
    $Detail = $DOM->getElementsByTagName('td');

    //#Get header name of the table
    foreach($Header as $NodeHeader) 
    {
        $aDataTableHeaderHTML[] = trim($NodeHeader->textContent);
    }
    //print_r($aDataTableHeaderHTML); die();

    //#Get row data/detail table without header name as key
    $i = 0;
    $j = 0;
    foreach($Detail as $sNodeDetail) 
    {
        $aDataTableDetailHTML[$j][] = trim($sNodeDetail->textContent);
        $i = $i + 1;
        $j = $i % count($aDataTableHeaderHTML) == 0 ? $j + 1 : $j;
    }
    //print_r($aDataTableDetailHTML); die();
    
    //#Get row data/detail table with header name as key and outer array index as row number
    for($i = 0; $i < count($aDataTableDetailHTML); $i++)
    {
        for($j = 0; $j < count($aDataTableHeaderHTML); $j++)
        {
            $aTempData[$i][$aDataTableHeaderHTML[$j]] = $aDataTableDetailHTML[$i][$j];
        }
    }
    $aDataTableDetailHTML = $aTempData; unset($aTempData);
    print_r($aDataTableDetailHTML); die();
?>

……………………………………………………………………………………………………………………………………………………………………………………

2

Answers


  1. I’ll do this just because it was fun using explode and str_replace — doing it without a PHP DOM parser ..

    Basically create a starting Main empty array using explode( '</tr>', $table );, and loop through it, add the temp arrays to it after stripping unwanted content (IE <tr> and trimming)

    <?php
    
    $table = <<<HTML
    <table>
      <tr>
        <th>Name</th>
        <th>Value</th>
      </tr>
      <tr>
        <td>Name One</td>
        <td>Value One</td>
      </tr><tr>
        <td>Name Two</td>
        <td>Value Two</td>
      </tr><tr>
        <td>Name Three</td>
        <td>Value Three</td>
      </tr>
    </table>
    HTML;
    
    $rows = explode( '</tr>', $table );
    array_shift($rows);
    array_pop($rows);
    
    $main_arr = [];
    
    foreach ($rows as $row){
      $name = trim( str_replace(['<td>', '<tr>'], '', explode('</td>', $row)[0] ) );
      $value = trim( str_replace(['<td>', '<tr>'], '', explode('</td>', $row)[1] ) );
    
      $tmp_arr = [];
      $tmp_arr['name'] = $name;
      $tmp_arr['value'] = $value;
    
      $main_arr[] = $tmp_arr;
    
    }
    
    print_r($main_arr);
    

    Your output should be:

    Array
    (
        [0] => Array
            (
                [name] => Name One
                [value] => Value One
            )
    
        [1] => Array
            (
                [name] => Name Two
                [value] => Value Two
            )
    
        [2] => Array
            (
                [name] => Name Three
                [value] => Value Three
            )
    
    )
    

    UPDATE

    Here is the PHP DOM code that does the same thing:

    <?php
    
    $DOM = new DOMDocument();
    $DOM->loadHTML("<table>
      <tr>
        <th>Name</th>
        <th>Value</th>
      </tr>
      <tr>
        <td>Name One</td>
        <td>Value One</td>
      </tr><tr>
        <td>Name Two</td>
        <td>Value Two</td>
      </tr><tr>
        <td>Name Three</td>
        <td>Value Three</td>
      </tr>
    </table>");
    $main_arr = [];
    $rows = $DOM->getElementsByTagName("tr");
    for ($i = 0; $i < $rows->length; $i++) {
        $cols = $rows->item($i)->getElementsbyTagName("td");
        $tmp_arr = [];
        if ($cols->item(0)->nodeValue){
          $tmp_arr['name'] = $cols->item(0)->nodeValue;
          $tmp_arr['value'] = $cols->item(1)->nodeValue;
          $main_arr[] = $tmp_arr;
        }
    }
    
    print_r( $main_arr );
    
    Login or Signup to reply.
  2. Your code is working too hard to try to keep the columnar data with the respective row.

    To make things easier, iterate the row (<tr>) elements, then access the elements within the given row.

    Code (Demo)

    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $result = [];
    foreach ($dom->getElementsByTagName('tr') as $row) {
        $result[][$row->getElementsByTagName('th')->item(0)->nodeValue] = $row->getElementsByTagName('td')->item(0)->nodeValue;
    }
    var_export($result);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search