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


remove first or specific child node xpath

此代码获取表格。

我想删除表中的第一个和第二个tr标记。

$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——

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

如何在一个2d数组中同时回显两个tr。例如。

       Array(
      [0] => Array(
     //array
                  )
}  

感谢

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

 $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
  }