duandi6531 2011-11-07 21:18
浏览 24
已采纳

遍历每个元素

Right now I access each row and cells of a table like this:

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

I was wondering if there's a more efficient way of doing this? Like maybe if there's a way to find out the last index number then I can probably do something like this:

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

Anybody knows how to find the last index (total number of rows in a table) ?

  • 写回答

1条回答 默认 最新

  • donglei7152 2011-11-07 21:26
    关注

    You can use the size() method.

    $last_index = pq('#id tr')->size() - 1; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思