douyan5481 2013-07-03 14:57
浏览 30
已采纳

按索引获取元素,[n]

I'm using PHPQuery to read some content from HTML, I'm unable to get the element by it's index using the square bracket notation.

See this simple example:

$html = '<div><table id="theTable"><tr><td>FIRST TD</td><td>SECOND TD</td><td>THIRD TD</td></tr></table></div>';

$pq = phpQuery::newDocumentHTML($html);

$table = $pq->find('#theTable');
$tds = $table->find('td');

echo "GETTING BY INDEX:

";
echo '$tds[1] = ' . $tds[1];

echo "


";

echo "GETTING IN FOREACH:

";
foreach($tds as $key => $td){
    echo '$tds[' . $key . '] = ' . pq($td) . "
";
}

The output of this is:

GETTING BY INDEX:

$tds[1] =

GETTING IN FOREACH:

$tds[0] = FIRST TD

$tds[1] = SECOND TD

$tds[2] = THIRD TD

I would have expected that I can get the contents of $tds[1] using square brackets, but seems not. How can I get it by index?

  • 写回答

3条回答

  • duanjiao2978 2013-07-03 15:06
    关注

    Found the answer just after posting the question. Instead of square brackets you need to use eq(n):

    echo '$tds[1] = ' . $tds->eq(1);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试