2014-07-16 64 views
0

我有兩個foreach循環,我希望匹配第一個和第二個的輸出。第一個從起始頁面抓取標題,第二個foreach導航到子頁面進行描述。我似乎不能輸出這兩個內聯,使標題顯示與相關的描述。將一個foreach的元素合併爲另一個

//1st foreach: 

$title = array(); 
foreach ($html->find('.event-box .event-details h1') as $t){ 
     $title[] = $t->plaintext; 
    } 
    echo implode('<br>',$title);'<br>'; 
//this lists all $title found on page ahead of the below foreach (obvisouly) 

//2nd foreach: 

    // loop through each link 
foreach ($anchors as $anchor) { 
    // Create the new link to follow and parse 
    $urlTemp = $baseUrl . $anchor->href; 
    $html2 = new simple_html_dom(); 
    $html2->load_file($urlTemp); 

    //Descriptions 
    foreach($html2->find('div[id=content-area]') as $article) { 
    } $para = array(); 
     foreach($article->find('p') as $p) { 
       $para[] = $p->innertext; } 


    echo 'DESC: '.implode('<br>',$para);'<br>'; 
    echo "<hr/>"; 

    $html2->clear(); 
    unset($htm2); 
} 

我想獲得的輸出順序所以$title遵循符合$para每個迭代。 $title需要與$para

$title //From first foreach - 1st in $title[] 
$para //From second foreach - 1st in $para[] 
</hr> 
$title //From first foreach - 2nd in $title[] 
$para //From second foreach - 2nd in $para[] 
</hr> 
$title 
$para 

匹配....等

回答

0

我通過添加索引關鍵的foreach解決了這個。像:

$i=0; 

    foreach ($anchors as $key=>$anchor) { 
    //........... 
    echo 'TITLE: '.$title[$i].'<br>'; 
    //........... 
    $i++; 

即什麼從第一的foreach數組元素中的第二$title[1], $title[2]的索引關鍵字匹配等可以用來達到的$title[$i]

相關問題