Search code examples
phphtmlarraysdomsimple-html-dom

Php Simple Dom Parser Unable to store data in array


I'm using Simple Html Dom to read an external site and its underpage to get some information for some car objects, either way, when echoing everything I've found with simple html the page loads in about 10 second which is expected but when i try to save it inside an array the page gets stuck and memory gets eaten.

$html = file_get_html($dealer);
$cars = $html->find('div[id=item_list]',0);
foreach($cars->find('a.item_link') as $carObj){
     $carlink = $carObj->href;
     $carhtml = file_get_html($carlink); 
     $cardata = $carhtml->find('div[id=view_body] div.span14',0);
     (string) $title = $carhtml->find('div[id=view_header] div h2',0);

if i put this row after the code it gets stuck in a infinite loop

     //$carsarray[] = array('ctitle' => $title); 

but echoing the same variable works with no problem

     echo $title;

rest of the code

     unset($images);
     $cardata->clear();

     unset($cardata);

}
$cars->clear();
unset($cars);

Solution

  • You should already be able to use the variable like an array. Its not really an array though, its a DOM object that Simple Dom Parser creates. I'm not sure why, but it causes that infinite loop problem when looped through a certain way. Making good use of $yourobject->find to keep traversing down the DOM, and $yourobject->plaintext method worked for me.

    Looks like in your case $carsarray[] = array('ctitle' => $title->plaintext); might do the trick. Hope this helps.