martincarlin87
martincarlin87

Reputation: 11042

PHP Simple HTML DOM Parser - Combining Two Arrays

What I am trying to do is scrape a page on Trip Advisor - I have what I need from the first page and then I do another loop to get the contents from the next page but when I try and add these details to the existing array it doesn't work for some reason.

error_reporting(E_ALL);
include_once('simple_html_dom.php');

$html = file_get_html('http://www.tripadvisor.co.uk/Hotels-g186534-c2-Glasgow_Scotland-Hotels.html');

$articles = '';

// Find all article blocks
foreach($html->find('.listing') as $hotel) {
    $item['name']     = $hotel->find('.property_title', 0)->plaintext;
    $item['link']     = $hotel->find('.property_title', 0)->href;

    $item['rating']    = $hotel->find('.sprite-ratings', 0)->alt;
    $item['rating']    = explode(' ', $item['rating']);
    $item['rating']    = $item['rating'][0];

    $articles[] = $item;
}

foreach($articles as $article) {

    echo '<pre>';
    print_r($article);
    echo '</pre>';

   $hotel_html = file_get_html('http://www.tripadvisor.co.uk'.$article['link'].'/');

   foreach($hotel_html->find('#MAIN') as $hotel_page) {
       $article['address']            = $hotel_page->find('.street-address', 0)->plaintext;
       $article['extendedaddress']    = $hotel_page->find('.extended-address', 0)->plaintext;
       $article['locality']           = $hotel_page->find('.locality', 0)->plaintext;
       $article['country']            = $hotel_page->find('.country-name', 0)->plaintext;

       echo '<pre>';
       print_r($article);
       echo '</pre>';

       $articles[] = $article;
    }
}

echo '<pre>';
print_r($articles);
echo '</pre>';

Here is all the debugging output that I get: http://pastebin.com/J0V9WbyE

URL: http://www.4playtheband.co.uk/scraper/

Upvotes: 0

Views: 1127

Answers (1)

LonWolf
LonWolf

Reputation: 26

I would change

$articles = '';

to:

$articles = array();

Before foreach():

$articlesNew = array();

When iterating over the array, insert in the new array

$articlesNew[] = $article;

At the end merge the arrays

$articles = array_merge($articles, $articlesNew);

Source: http://php.net/manual/en/function.array-merge.php for more array php merge / combine.

I never tried to alter an array when already iterating through it in PHP, but if you did this with C++ collections improperly it would crash unless you treat fatal exceptions. My wild guess is that you shouldn't alter the array while iterating it. I know i would never do that. Work with another variable.

Upvotes: 1

Related Questions