将PHP表转换为数组

将PHP表转换为数组,php,html,Php,Html,我试图将一个表格从HTML文件读入一个数组,我被卡住了。 任何帮助都将不胜感激 每个表元素应存储在1个数组值中 示例:$arr[1]=derhe1ges1 PHP loadHTMLFile(“106642new.html”); //丢弃空白 $dom->preserveWhiteSpace=false; //通过其标记名创建表 $tables=$dom->getElementsByTagName('table'); //从表中获取所有行 $rows=$tables->item(0)->getEl

我试图将一个表格从HTML文件读入一个数组,我被卡住了。 任何帮助都将不胜感激

每个表元素应存储在1个数组值中

示例:
$arr[1]=derhe1ges1

PHP

loadHTMLFile(“106642new.html”);
//丢弃空白
$dom->preserveWhiteSpace=false;
//通过其标记名创建表
$tables=$dom->getElementsByTagName('table');
//从表中获取所有行
$rows=$tables->item(0)->getElementsByTagName('tr');
//$test=$tables->item(0)->getElementsByTagName('td');
//在表行上循环
foreach($行作为$行){
//按标记名获取每个列
$cols=$row->getElementsByTagName('td');
$i=$i+1;
$value=“numer:”.$i.:“$cols->item(0)->nodeValue.PHP\u EOL;
//$value=“test:”.$i.:“$cols->item(0)->nodeValue.PHP\u EOL;
$cols=数组(1,2,3,4,5);
echo美元价值;
//$cols[$i]=$row;
//响应这些值
//echo$cols->item(0)->nodeValue;
}   
?>
HTML:


 

Basisrooster schooljaar 2011 2012(m.i.v.12-09-11)


马恩达格 丁斯达克 沃恩斯达格 东德达格 维里达格 1e uur     是   HE09   经济 5. 威克   HC17   生物 4. 奥特   HC01   杜特尔 6.     2e uur 凯吉   HC02   维萨 3. 快速公交   HE05   内特 奥特   HC01   杜特尔 6. BAU   HG01   瞧 遇见   HD02   entl 3e uur 是   HE07   经济 5. 遇见   HD02   entl 是   HE05   经济 5. BAU   HG01   瞧 凯吉   HC02   维萨 3. 4e uur     德   HE08   ges 1. 凯吉   HC06   维萨 3. 德   呵呵   ges 1. CHR   HB15   ckv 5e uur 文件   HE09   并购 2.     遇见   HD02   entl 快速公交   HE05   内特 奥特   HC03   杜特尔 6. 6e uur 奥特   HC03   杜特尔 6.
如果您认为问题在于您的第一个表是其他表的容器。 如果要获取所有表的内容,还应该遍历表列表

如果您只想获取内部表的内容,那么首先尝试在DOM中定位它。我建议找到第一个表,而不是获取其中的所有表元素并遍历它们

var_dump是调试的良好起点,您不需要任何东西
<?php
      libxml_use_internal_errors(true);
      $i=0;
      // new dom object  
      $dom = new DOMDocument();  

      //load the html  
      $html = $dom->loadHTMLFile("106642new.html");  

      //discard white space   
      $dom->preserveWhiteSpace = false;   

      //the table by its tag name  
      $tables = $dom->getElementsByTagName('table');   

      //get all rows from the table  
      $rows = $tables->item(0)->getElementsByTagName('tr');   
      // $test = $tables->item(0)->getElementsByTagName('td');   

      // loop over the table rows  
      foreach ($rows as $row) {
          // get each column by tag name  
          $cols = $row->getElementsByTagName('td');  
          $i= $i + 1 ;
          $value = "Nummer: ".$i.":  ".$cols->item(0)->nodeValue.PHP_EOL;
          // $value = "test: ".$i.":  ".$cols->item(0)->nodeValue.PHP_EOL;
          $cols = array(1, 2, 3, 4, 5);
          echo $value;
          //  $cols[$i] = $row; 
          // echo the values    
          //echo $cols->item(0)->nodeValue ; 
      }   
?>
<body bgcolor="#FFFFFF" topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">

          <div align=left>

          <table BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH="100%" height="100%">

          <tr><td valign="top">&nbsp</td></tr>

          <tr><td valign="top">

          <p font class="Header">Basisrooster schooljaar 2011 2012 (m.i.v. 12-09-11)</font></p>
          <br><div font class="lNameHeader"> </font> </div><table border=1>
          <tr class="AccentDark">
           <td align="left" width="65" class="tableHeader"></td>
           <td align="center" width="auto" class="tableHeader">Maandag</td>
           <td align="center" width="auto" class="tableHeader">Dinsdag</td>
           <td align="center" width="auto" class="tableHeader">Woensdag</td>
           <td align="center" width="auto" class="tableHeader">Donderdag</td>
           <td align="center" width="auto" class="tableHeader">Vrijdag</td>
          </tr><tr>
           <td align="left" width="50" class="tableHeader">1e uur</td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell"></td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell"></td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell"></td>
           <td align="left" width="9" class="tableCell"></td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">WAS</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HE09</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">econ</td>
           <td align="left" width="9" class="tableCell">5</td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">WIK</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HC17</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">biol</td>
           <td align="left" width="9" class="tableCell">4</td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">OTT</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HC01</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">dutl</td>
           <td align="left" width="9" class="tableCell">6</td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell"></td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell"></td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell"></td>
           <td align="left" width="9" class="tableCell"></td>
          </tr>
          </table>
          </td>
          </tr>
          <tr>
           <td align="left" width="50" class="tableHeader">2e uur</td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">KEJ</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HC02</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">wisA</td>
           <td align="left" width="9" class="tableCell">3</td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">BRT</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HE05</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">netl</td>
           <td align="left" width="9" class="tableCell"></td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">OTT</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HC01</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">dutl</td>
           <td align="left" width="9" class="tableCell">6</td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">BAU</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HG01</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">lo</td>
           <td align="left" width="9" class="tableCell"></td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">MET</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HD02</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">entl</td>
           <td align="left" width="9" class="tableCell"></td>
          </tr>
          </table>
          </td>
          </tr>
          <tr>
           <td align="left" width="50" class="tableHeader">3e uur</td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">WAS</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HE07</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">econ</td>
           <td align="left" width="9" class="tableCell">5</td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">MET</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HD02</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">entl</td>
           <td align="left" width="9" class="tableCell"></td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">WAS</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HE05</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">econ</td>
           <td align="left" width="9" class="tableCell">5</td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">BAU</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HG01</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">lo</td>
           <td align="left" width="9" class="tableCell"></td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">KEJ</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HC02</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">wisA</td>
           <td align="left" width="9" class="tableCell">3</td>
          </tr>
          </table>
          </td>
          </tr>
          <tr>
           <td align="left" width="50" class="tableHeader">4e uur</td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell"></td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell"></td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell"></td>
           <td align="left" width="9" class="tableCell"></td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">DER</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HE08</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">ges</td>
           <td align="left" width="9" class="tableCell">1</td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">KEJ</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HC06</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">wisA</td>
           <td align="left" width="9" class="tableCell">3</td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">DER</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HE10</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">ges</td>
           <td align="left" width="9" class="tableCell">1</td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">CHR</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HB15</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">ckv</td>
           <td align="left" width="9" class="tableCell"></td>
          </tr>
          </table>
          </td>
          </tr>
          <tr>
           <td align="left" width="50" class="tableHeader">5e uur</td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">DOC</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HE09</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">m&o</td>
           <td align="left" width="9" class="tableCell">2</td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell"></td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell"></td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell"></td>
           <td align="left" width="9" class="tableCell"></td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">MET</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HD02</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">entl</td>
           <td align="left" width="9" class="tableCell"></td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">BRT</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HE05</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">netl</td>
           <td align="left" width="9" class="tableCell"></td>
          </tr>
          </table>
          </td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">OTT</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HC03</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">dutl</td>
           <td align="left" width="9" class="tableCell">6</td>
          </tr>
          </table>
          </td>
          </tr>
          <tr>
           <td align="left" width="50" class="tableHeader">6e uur</td>
           <td align="left" width="auto" class="tableCell"><table border="0" cellpadding="0" cellspacing="0" >
          <tr>
           <td align="left" width="41" class="tableCell">OTT</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="75" class="tableCell">HC03</td>
           <td align="left" width="3" class="tableCell">&nbsp</td>
           <td align="left" width="73" class="tableCell">dutl</td>
           <td align="left" width="9" class="tableCell">6</td>
          </tr>
          </table>
          </td>
<p font class="Header">Basisrooster schooljaar 2011 2012 (m.i.v. 12-09-11)</font></p>
<p class="Header">Basisrooster schooljaar 2011 2012 (m.i.v. 12-09-11)</p>