2017-07-28 79 views
-2

我通过报废获取数据。 数据源是一个表,我需要获取每个(tr)的数据。如何循环DOM元素并将其作为数组存储?

该表具有3(TD),它是:

  • 标题
  • 日期
  • 链路

这里是我使用的代码:

$data = array(); 
$counter = 1; 
$index = 0; 

foreach($html->find('#middle table tr td') as $source){ 

    $dont_include = array(
     '<td>CONTAIN TEXT THAT I DONT WNAT TO INCLUDE IN HERE</td>' 
    ); 

    if (!in_array($source->outertext, $dont_include)) { 

     // IF IT CONTAIN LINK THEN GET IT LINK 
     // THE SOURCE DATA FOR LINK IS SOMETHING LIKE 
     // <td><a href="">xx</a></td> 
     if(strstr($source->innertext, 'http://')){ 

       $a = new SimpleXMLElement($source->innertext); 

       $the_link = (string) $a['href'][0]; 
       $data[$index] = array('link' => $the_link);; 
     }else{ 
      if ($counter==2) { 
       $data[$index] = array('title' => $source->innertext); 
      }else{ 
       $data[$index] = array('date' => $source->innertext); 
       $counter = 0; 
       $index++; 
      } 
     } 
    } 
    $counter++; 
} 

print_r($data); 

问题: 我该怎么办采用这种结构存储在数组中的这些值:

Array (
    [0] => Array (
     [title] => "" 
     [date] => "" 
     [link] => "" 
    ) 
    [1] => Array (
     [title] => "" 
     [date] => "" 
     [link] => "" 
    ) 
    ... 
) 

更新,这里是源结构:

<!-- THIS IS THE SOURCE , AT THE TOP HERE CONTAIN TD THAT I DONT WANT --> 
    <td>title</td> 
    <td class="ac">date</td> 
    <td width="190"><a href="i need this link" target="_blank">filename , i dont need the file name</a> 
    </td> 
<td>title</td> 
    <td class="ac">date</td> 
    <td width="190"><a href="i need this link" target="_blank">filename , i dont need the file name</a> 
    </td> 
<td>title</td> 
    <td class="ac">date</td> 
    <td width="190"><a href="i need this link" target="_blank">filename , i dont need the file name</a> 
    </td> 
<td>title</td> 
    <td class="ac">date</td> 
    <td width="190"><a href="i need this link" target="_blank">filename , i dont need the file name</a> 
    </td> 
+0

'title','date','link'就是你的td数据我对吗? –

+0

'tr'可能包含'td'或'th' –

+0

您能否详细描述您的问题.. –

回答

1

,而不是遍历td我建议你遍历tr这样就可以创建你的数组。试试这个

$rowData = array(); 

foreach ($html->find('#middle table tr') as $rows) { 
    $cellData = array(); 

    $cellData['title'] = $rows->children(0)->innertext; 
    $cellData['date'] = $rows->children(1)->innertext; 
    $cellData['link'] = $rows->children(2)->innertext; 

    $rowData[] = $cellData; 
} 
print_r($rowData); 
+0

投票的原因是什么?我已经测试这个,它的工作正常 –

+0

雅可能是选民不知道HTML DOM内容 –

+0

也不是我的投票,我提供了源数据的问题:) – taek

相关问题