删除第一个或特定的子节点

删除第一个或特定的子节点

本文介绍了删除第一个或特定的子节点 xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码获取表格.

我想删除表格中的第一个和第二个 tr 标签.

I want to remove first and second tr tag in the table.

$data = array();
$table_rows = $xpath->query('//table[@class="adminlist"]/tr');
if($table_rows->length <= 0) { // exit if not found
echo 'no table rows found';
exit;
}

foreach($table_rows as $tr) { // foreach row
 $row = $tr->childNodes;
if($row->item(0)->tagName != 'tblhead') { // avoid headers
    $data[] = array(
        'Name' =>trim($row->item(0)->nodeValue),
        'LivePrice' => trim($row->item(2)->nodeValue),
        'Change'=> trim($row->item(4)->nodeValue),
        'Lowest'=> trim($row->item(6)->nodeValue),
        'Topest'=> trim($row->item(8)->nodeValue),
        'Time'=> trim($row->item(10)->nodeValue),
    );
}
}

和问题 2

在下面的表格tr中有两个类--- EvenRow_Print 和 OddRow_Print ---

In the bellow table tr have two class --- EvenRow_Print and OddRow_Print ---

     $data = array();
     $table_rows = $xpath->query('//table/tr');
     if($table_rows->length <= 0) {
     echo 'no table rows found';
     exit;
          }

    foreach($table_rows as $tr) { // foreach row
 $row = $tr->childNodes;
if($row->item(0)->tagName != 'tblhead') { // avoid headers
    $data[] = array(
        'Name' =>trim($row->item(0)->nodeValue),
        'LivePrice' => trim($row->item(2)->nodeValue),
        'Change'=> trim($row->item(4)->nodeValue),
        'Lowest'=> trim($row->item(6)->nodeValue),
        'Topest'=> trim($row->item(8)->nodeValue),
        'Time'=> trim($row->item(10)->nodeValue),
    );
   }
 }

如何在一个二维数组中同时回显 tr .例子.

How can I echo both tr in one 2d array .examp.

       Array(

      [0] => Array(
     //array
                  )

}

谢谢

推荐答案

对于问题 1 - 有不同的方法可以跳过第一个和最后一个元素,例如使用 array_shift() 删除第一个条目,使用 array_pop() 删除最后一个条目.但是由于不清楚保持数组原样是否更好,因此可以通过简单的方式跳过 foreach 中的两个条目,例如使用计数器,继续第一个条目并打破最后:

For question 1 - there are different ways to skip the first and last element, e.g. removing the first entry using array_shift() and the last entry using array_pop(). But as it's not clear if it'd be better to keep the array as it is, it's possible to skip both entries in the foreach in an easy way like using a counter, continuing for the first entry and breaking for the last:

 $i = 0;
 $trlength = count($table_rows);
 foreach( ...) {
   if ($i == 0)  // is true for the first entry
   {
     $i++;       // increment counter
     continue;   // continue with next entry
   }
   else if ($i == $trlength - 1)   // last entry, -1 because $i starts from 0
   {
     break;      // exit foreach loop
   }
   ....         // handle all other entries
   $i++;        // increment counter in foreach loop
  }

这篇关于删除第一个或特定的子节点 xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:02