英文:
Cannot print values from a table` columns using PHP DOM
问题
我正在尝试使用DOM库打印这个表格:
我希望从每一列中获取所有的值,可能存储在一个数组中。以下是迄今为止我的代码:
foreach ($descrDom->find(".tbl-fit") as $key => $td) {
$descr = $td->plaintext . "\n";
//var_dump($descr);
}
目前,我的代码正在打印表格中每个HTML元素的所有数据,包括表头。
英文:
I am trying to print this table using the DOM library:
I want all the values from each column to be printed separately, probably in an array. Here is my code so far:
foreach ($descrDom->find(".tbl-fit") as $key => $td) {
$descr=$td->plaintext ."\n";
//var_dump($descr);
}
At this point my code is printing all the data from each html element in the table including the table headers.
答案1
得分: 0
以下是您要的翻译部分:
这样的意思是
$column= 0; // 您想要打印的列号
$cell = 1;
while ($b = $descrDom->find("table.tbl-fit tr", $cell)) {
$result[] = $b->children($column)->plaintext;
$cell++;
}
print_r($result);
**输出:**
ALFA ROMEO MITO (955_)
ALFA ROMEO MITO (955_)
FIAT 500 (321_)
FIAT 500 (321_)
FIAT 500 C(321_)
.. 等等
如果您想获取类型列,只需将`$column`变量更改为1,以此类推。
英文:
You mean like this
$column= 0; // The column number you want to be printed
$cell = 1;
while ($b = $descrDom->find("table.tbl-fit tr", $cell)) {
$result[] = $b->children($column)->plaintext;
$cell++;
}
print_r($result);
Output:
ALFA ROMEO MITO (955_)
ALFA ROMEO MITO (955_)
FIAT 500 (321_)
FIAT 500 (321_)
FIAT 500 C(321_)
.. etc
If you want to fetch the type column just change $column
variable to 1 and so on.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论