2011-11-07 183 views
3

现在我访问表的每一行和单元格这样的:遍历每个元素

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

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

回答

4

您可以使用size()方法。

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