【问题标题】:Traversing each element遍历每个元素
【发布时间】:2011-11-07 21:18:34
【问题描述】:

现在我像这样访问表格的每一行和单元格:

$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)'];        
}

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

【问题讨论】:

    标签: php jquery dom phpquery


    【解决方案1】:

    您可以使用size() 方法。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 2018-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多