编程语言
首页 > 编程语言> > php-遍历每个元素

php-遍历每个元素

作者:互联网

现在,我访问表的每一行和单元格,如下所示:

$f = phpQuery::newDocumentFile('test.html');

// access row #1
$o = $f['#id tr:eq(0)'];
$d = phpQuery::newDocument($o);

// get cells from row #1
$arr[0]['c1'] = $d['td:eq(0)'];
$arr[0]['c2'] = $d['td:eq(1)'];

// access row #2
$o = $f['#id tr:eq(1)'];
$d = phpQuery::newDocument($o);

// get cells from row #2
$arr[1]['c1'] = $d['td:eq(0)'];
$arr[1]['c2'] = $d['td:eq(1)'];

我想知道是否有更有效的方法?就像也许如果有一种方法可以找到最后一个索引号,那么我可能可以做这样的事情:

$f = phpQuery::newDocumentFile('test.html');

$last_index = 10;

for ($i = 0; $i <= $last_index; $i++)
{
    $o = $f['#id tr:eq($i)'];
    $d = phpQuery::newDocument($o);     

    $arr[$i]['c1'] = $d['td:eq(0)'];
    $arr[$i]['c2'] = $d['td:eq(1)'];        
}

有人知道如何找到最后一个索引(表中的总行数)吗?

解决方法:

您可以使用size()方法.

$last_index = pq('#id tr')->size() - 1; 

标签:phpquery,dom,php,jquery
来源: https://codeday.me/bug/20191102/1988236.html