【发布时间】:2015-12-02 11:05:28
【问题描述】:
基本上,我正在尝试从多个 xPath 查询中提取数据,并将从每个文件中提取的数据提交到 CSV 中的一行。我遇到的问题是遍历特定的 xPath 查询并将其结果连接到一行。
我有以下代码:
$category_titles = $xpath->query('//*[@id="shopMain"]/div/div/h1');
$category_introduction = $xpath->query('//*[@id="shopMain"]/div/div/p');
if($category_title){
foreach($category_title){
foreach($category_introduction as $node){
$test .= $node->nodeValue;
var_dump($test);
continue();
}
$test = '':
}
}
我正在尝试为每个$category_title 循环$category_introduction 查询并连接每个<p> 元素。然后,当所有<p> 标签都已连接时,我尝试跳出循环,并仅在清除$test 的情况下重复该过程。谁能建议我如何在嵌套的 foreach 循环中执行此操作?我从来没有写过嵌套循环,现在很挣扎。
【问题讨论】: