php - How to loop DOM elements and store as an array? -


i'm getting data via scrapping. data source table , need data every (tr).

the table has 3 (td) is:

  • title
  • date
  • link

here code use:

$data = array(); $counter = 1; $index = 0;  foreach($html->find('#middle table tr td') $source){      $dont_include = array(         '<td>contain text dont wnat include in here</td>'     );      if (!in_array($source->outertext, $dont_include)) {          // if contain link link         // source data link          // <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); 

the problem : how can store these values in array using structure:

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

update, here source structure :

    <!-- source , @ top here contain td dont want -->     <td>title</td>     <td class="ac">date</td>     <td width="190"><a href="i need link" target="_blank">filename , dont need file name</a>     </td> <td>title</td>     <td class="ac">date</td>     <td width="190"><a href="i need link" target="_blank">filename , dont need file name</a>     </td> <td>title</td>     <td class="ac">date</td>     <td width="190"><a href="i need link" target="_blank">filename , dont need file name</a>     </td> <td>title</td>     <td class="ac">date</td>     <td width="190"><a href="i need link" target="_blank">filename , dont need file name</a>     </td> 

instead of loop through td suggest loop through tr can create array. try this

$rowdata = array();  foreach ($html->find('#middle table tr') $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); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -