【发布时间】:2014-07-16 02:24:25
【问题描述】:
我有两个 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
....等
【问题讨论】:
标签: php arrays foreach simple-html-dom