我试图在数据库中插入一个表,我想在一个数组中转换两行。谁能帮我?
<table>
<tr class="pair"><td>1</td><td>2</td></tr>
<tr class="pair">td<>3</td><td>4</td></tr>
<tr class="unpair"><td>1</td><>2</td></tr>
<tr class="unpair"><td>3</td><td>4</td></tr>
</table>
<?php
require('simple_html_dom.php');
foreach($table->find('tr[class=pair') as $rowpair) {
$rowData = array();
foreach($rowpair->find('td') as $cell) {
$rowData[] = $cell->innertext;
}
foreach($table->find('tr[class=unpair') as $rowunpair) {
$rowData = array();
foreach($rowunpair->find('td') as $cell) {
$rowData[] = $cell->innertext;
}
?>
要获得
<table>
<tr class="pair"><td>1</td><td>2</td><td>3</td><td>4</td></tr>
<tr class="unpair"><td>1</td><td>2</td><td>3</td><td>4</td></tr>
</table>
这应该可以按类对所有表行进行分组。
基本逻辑是遍历表中的所有行,并确定之前是否见过该类。如果没有,它会将对对该行的引用存储为要使用的"规范"行。如果它以前见过该类,它将把它的所有子类转移到规范行。
此方法应适用于博客中任意数量的表和任何一组类名。
<?php
$str = '<table><tr class="pair"><td>1</td><td>2</td></tr><tr class="pair"><td>3</td><td>4</td></tr><tr class="unpair"><td>1</td><td>2</td></tr><tr class="unpair"><td>3</td><td>4</td></tr>
</table>';
$doc = new DOMDocument();
$doc->loadHTML($str);
$tables = $doc->getElementsByTagName('table');
foreach ($tables as $table) {
#For each TR in the table, group into rows
$table_classes = array();
$rows = $table->getElementsByTagName('tr');
$row_list = array();
foreach ($rows as $row) {
array_push($row_list, $row);
}
for($i=0; $i<count($row_list); $i++){
$row = $row_list[$i];
$row_class = $row->getAttribute('class');
if(!array_key_exists($row_class, $table_classes)){
#if this is the for occurrence of that clase, store this row as the original_row
$table_classes[$row_class] = $row;
}else{
$original_row = $table_classes[$row_class];
#Move children over to original row
foreach ($row->childNodes as $child) {
$clone = $child->cloneNode(true);
$original_row->appendChild($clone);
}
#Now delete original
$row->parentNode->removeChild($row);
}
}
}
echo htmlspecialchars($doc->saveXML());
?>
返回:
<table>
<tr class="pair">
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
<tr class="unpair">
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</table>