我尝试过以下代码:
$car_row = $car_xpath->query('//h3[@class="adtitlesnb"]');
$car_row2 = $car_xpath->query('//div[@class="snb_price_tag"]');
$i = 0;
echo "<table><thead><tr><td>Car Name</td><td>Price</td></tr></thead><tbody>";
foreach($car_row as $row){
echo "<tr><td>";
echo $row->nodeValue;
echo "</td><td>";
echo $car_row2->nodeValue;
echo "</td></tr>";
}
echo "</tbody></table>";
答案 0 :(得分:0)
你不能用foreach迭代一个数组,并期望另一个跟随。
foreach
基本上会重置数组上的当前索引,然后循环调用数组上的next,直到没有剩余元素。使用reset
和next
您可以为您的第二个数组模拟此项,如下所示:
$car_row = $car_xpath->query('//h3[@class="adtitlesnb"]');
$car_row2 = $car_xpath->query('//div[@class="snb_price_tag"]');
echo "<table><thead><tr><td>Car Name</td><td>Price</td></tr></thead><tbody>";
$row2 = reset($car_row2); // set the internal array pointer to the begining
foreach($car_row as $row) {
echo "<tr><td>";
echo $row->nodeValue;
echo "</td><td>";
echo $row2->nodeValue;
echo "</td></tr>";
$row2 = next($car_row2); // retrieve the next node from car_row2
}
echo "</tbody></table>";