2014-09-06 5 views
-1

이 코드 get table.첫 번째 또는 특정 하위 노드 xpath를 제거합니다.

테이블에서 첫 번째 및 두 번째 tr 태그를 제거하고 싶습니다. 벨로우즈 테이블 TR 2

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

및 질문은 두 개의 클래스 --- EvenRow_Print 및 OddRow_Print --- 내가 한 2 차원 배열의 두 TR 에코 어떻게

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

있습니다. examp.

 Array(

     [0] => Array(
    //array 
       ) 

} 

감사의

+0

질문 당 하나의 질문 만하십시오. 한 번에 두 가지 질문을하는 것은 Stackoverflow에서 작동하지 않습니다. – hakre

답변

1

질문 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 
    } 
+0

foreach에는 1 개가있는 경우에. foreach에서 if ($ i == 0)를 설정하는 방법을 모르겠습니다. –

+0

이미 가지고있는 경우 중요하지 않습니다. 전에 "if"위에 놓으십시오; 첫 번째 또는 마지막 항목 인 경우 foreach는 건너 뛰거나 중단됩니다. 그렇지 않은 경우에는 내 대답의 부분에 "... // 다른 모든 항목을 처리합니다"라는 부분을 추가해야하는 경우 다음 단계로 진행됩니다. –

관련 문제