结合2个独立的foreach循环

时间:2017-01-09 01:10:09

标签: php loops foreach scrape

到目前为止,我已经设法从外部网站抓取2个元素到我的测试页面: http://mt-cloud.co.uk/nhs/

(请在页面上进行测试搜索以查看结果)

$ch = curl_init('http://www.nhs.uk/service-search/GP/m410ux/Results/4/-2.35167407989502/53.4519462585449/4/0?distance=25');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
$output = curl_exec($ch);
curl_close($ch);

$document = new DOMDocument;
libxml_use_internal_errors(true);
$document->loadHTML($output);
$document->encoding = 'utf-8';
$xpath = new DOMXPath($document);
$pnames = $xpath->query("//th[@class='fctitle']");
$addresses = $xpath->query("//td[@class='fcdetails fc-first']");

我有2个foreach循环:

foreach ($pnames as $pname) {
$result1 = $document->saveHTML($pname);
}


foreach ($addresses as $address) {
$result2 = $document->saveHTML($address);
}

$ result1 = GP练习的名称 $ result2 = GP练习的地址

正如您在测试页面上看到的那样,我的结果1和2是分开的。我如何得到它们,所以我一起有练习名和练习地址?

更新(@Tri)

for($i = 0; $i < count($pnames); $i++){
$name= $document->saveHTML($pnames[$i]);
$name=str_replace ('<a href="/Services/', '<a href="http://www.nhs.uk/Services/', $name);
$address = $document->saveHTML($addresses[$i]);

echo $name.'<br>'.$address;
}

只返回一个结果而不是全部

这是我的完整PHP代码: http://mt-cloud.co.uk/nhs/content/code

数据图像我试图刮伤 http://mt-cloud.co.uk/nhs/content/results.png

2 个答案:

答案 0 :(得分:1)

你的第一个代码没问题,你只需要将你的名字和地址存储在一个二维数组中,然后遍历你的数组。

这段代码与你的完全相同:

$ch = curl_init('http://www.nhs.uk/service-search/GP/m410ux/Results/4/-2.35167407989502/53.4519462585449/4/0?distance=25');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
$output = curl_exec($ch);
curl_close($ch);

$document = new DOMDocument;
libxml_use_internal_errors(true);
$document->loadHTML($output);
$document->encoding = 'utf-8';
$xpath = new DOMXPath($document);
$pnames = $xpath->query("//th[@class='fctitle']");
$addresses = $xpath->query("//td[@class='fcdetails fc-first']");

现在我们将创建一个数组:

$results = array();

然后使用你的循环并在数组的相同索引中将名称和地址存储为对:

$iCnt = 0;
foreach ($pnames as $pname){
    $results[$iCnt]['name'] = $document->saveHTML($pname);
    $iCnt++;
}

$iCnt = 0;
foreach ($addresses as $address){
    $results[$iCnt]['address'] = $document->saveHTML($address);
    $iCnt++;
}

现在我们有一个包含名称和地址对的数组,如果我们遍历它,我们可以一起看到它们:

for($iCnt = 0, $cnt = count($results); $iCnt < $cnt; $iCnt++){
    echo 'Name: '.$results[$iCnt]['name'].'<br>';
    echo 'Address: '.$results[$iCnt]['address'].'<br>';
}

这就是全部。完整的代码如下所示:

<?php
$ch = curl_init('http://www.nhs.uk/service-search/GP/m410ux/Results/4/-2.35167407989502/53.4519462585449/4/0?distance=25');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
$output = curl_exec($ch);
curl_close($ch);

$document = new DOMDocument;
libxml_use_internal_errors(true);
$document->loadHTML($output);
$document->encoding = 'utf-8';
$xpath = new DOMXPath($document);
$pnames = $xpath->query("//th[@class='fctitle']");
$addresses = $xpath->query("//td[@class='fcdetails fc-first']");

$results = array();

$iCnt = 0;
foreach ($pnames as $pname){
    $results[$iCnt]['name'] = $document->saveHTML($pname);
    $iCnt++;
}

$iCnt = 0;
foreach ($addresses as $address){
    $results[$iCnt]['address'] = $document->saveHTML($address);
    $iCnt++;
}

for($iCnt = 0, $cnt = count($results); $iCnt < $cnt; $iCnt++){
    echo 'Name: '.$results[$iCnt]['name'].'<br>';
    echo 'Address: '.$results[$iCnt]['address'].'<br>';
}
?>

答案 1 :(得分:0)

所以不要使用foreach:

for($i = 0; $i < count($pnames); $i++){
    $name= $document->saveHTML($pnames[$i]);
   $address = $document->saveHTML($addresses[$i]);
   //do something with your result
}

注意:您必须确保地址长度等于pnames