1

I am trying to extract some info from a website using simple_html_dom.

Currently I am using:

foreach ($html->find('div.product') as $results) {
    foreach ($results->find('div.image') as $img) {
        echo $img;
    }
    foreach ($results->find('a.title') as $title) {
        echo $title->plaintext;
    }
    foreach ($results->find('div.price') as $price) {
        echo $price;
    }
}

Which works fine. However I need to be able to echo each variable outside of the foreach loop. If I do that using the above code, only the final result will be displayed, i.e. out of the 10 products I am trying to extract only the 10th will be displayed.

Is there a way I can use an array to store all the results from each foreach loop then echo them out once the overall loop is finished?

Something like this:

foreach ($html->find('div.product') as $results) {
    foreach ($results->find('div.image') as $img) {
        array($img);
    }
    foreach ($results->find('a.title') as $title) {
        array($title->plaintext);
    }
    foreach ($results->find('div.price') as $price) {
        array($price);
    }
}
echo array($img);
echo array($title);
echo array($price);

Sorry if this question is confusing I don't have the best grasp on PHP, especially arrays!

5
  • please provide some sample html to be parsed Commented Jan 31, 2012 at 14:26
  • (reference) php.net/arrays Commented Jan 31, 2012 at 14:31
  • and you do realize that $results->find already returns arrays, right? Commented Jan 31, 2012 at 14:32
  • I was using this: $html = file_get_html('amazon.co.uk/s/…); Commented Jan 31, 2012 at 15:00
  • No sorry I did not. I've never worked with PHP before at this level, so apologise if the question came across as stupid. Commented Jan 31, 2012 at 15:01

4 Answers 4

3
$array_img = array();
$array_title = array();
$array_price = array();
foreach ($html->find('div.product') as $results) {
    foreach ($results->find('div.image') as $img) {
        $array_img[] = $img;
    }
    foreach ($results->find('a.title') as $title) {
        $array_title[]= $title->plaintext;
    }
    foreach ($results->find('div.price') as $price) {
        $array_price[]= $price;
    }
}
echo '<pre>';
print_r($array_img);
print_r($array_title);
print_r($array_price);
echo '</pre>';
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the reply. I can see the data I want using this option, however it returns hundreds of lines of code around it. Is there anyway to only extract the specific info.
If it helps I am using this html: $html = file_get_html('amazon.co.uk/s/…);
When I used the first block of code in my question I would get the actual image of the product, the title, and the price. However when I use print_r($array_img) I get a lot of unwanted lines of code
you have to use keys for echo specific info that you want to use like $array_price[0] check out the arrays manuel from php.net php: Arrays
1
$images = array();
foreach ($html->find('div.product') as $results) {
    foreach ($results->find('div.image') as $img) {
        $images[] = $img; // append $img to the $images array
    }
}

var_dump($images);

Do the same for the title and price data as well.

8 Comments

Thanks for the reply. This outputs hundreds of lines of code similar to this "array(15) { [0]=> object(simple_html_dom_node)#653 (9) { ["nodetype"]=> int(1) ["tag"]=> string(3) "div"" Is there a way to only display the images? Thank a lot.
What images? Your find() isn't looking images, it's looking for divs with css class "image".
Also if it helps, this is the html I am using: $html = file_get_html('amazon.co.uk/s/…);
Yes I know, it looks for that div then pulls out whatever is in that div which is the actual image in this case.
Unless you've got more than the code you've shown above, then no, it doesn't pull out images. It's only looking for <div class="image"> and returning the matching DOM nodes.
|
1
$img = array();
$title = array();
$price = array();
foreach ($html->find('div.product') as $results) {
    $img[] = $results->find('div.image');
    $title[] = $results->find('a.title');
    $price[] = $results->find('div.price');
}

print_r($img);
print_r($title);
print_r($price);

Comments

0

Not sure if I completely understand you question, try the following.

$priceList = $titleList = $imgList = array();

foreach ($html->find('div.product') as $results) {<br/>
    foreach ($results->find('div.image') as $img) {<br/>
        $imgList[] = $img;<br/>
    }<br/>
    foreach ($results->find('a.title') as $title) {<br/>
        titleList[] = $title;<br/>
    }<br/>
    foreach ($results->find('div.price') as $price) {<br/>
        priceList[] = $price;<br/>
    }<br/>
}<br/>
foreach ($imgList as $img) {<br/>
        echo $img;<br/>
}<br/>

And so on...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.