0

我想从每 3 个单元格中取出索引为 0 的第一个单元格,第二个为 1,第三个为 2 并将其全部放入一个数组中,以便在 $ 结果 [0] 中的所有单元格值都是索引为 0 - 全部tr线等

<?
$lines = $xpath->query("//table[@id='cab_table'] //tr");
            $result = array();

                foreach($lines as $line) {

                    for($j=0; $j<=3; $j++) {

                     $tds_{$j} = $xpath->query('//td['.$j.']', $line);
                     $tds_{$j} = $xpath->query('//td['.$j.']', $line);
                     $tds_{$j} = $xpath->query('//td['.$j.']', $line);

                     $count = $tds_{$j}->length;

                        for($i=0; $i<$count; $i++){

                            $result['number'][] = $tds_{$j}->item($i)->nodeValue;
                            $result['volume'][] = $tds_{$j}->item($i)->nodeValue;
                            $result['code'][] = $tds_{$j}->item($i)->nodeValue;

                        }

                    }

?>              }

HTML 代码:

        <div class="pc-data-cab_table_block_container" style="overflow:auto">
            <div id="cab_table_block" style="width:100%;overflow:auto">
                <table id="cab_table" cellpadding="0px" cellspacing="0px" style="width:100%;overflow:auto">
                    <tr id="cab_table_tr_top">
                        <th id="cab_table_left">Номер счета&lt;/th>
                        <th>Объем</th>
                        <th id="cab_table_right">РЎСѓРјРјР° РєРѕРјРјРёСЃСЃРёРё</th>
                    </tr>
                                                                                <tr class="cab_table_tr2">
                            <td class="cab_table_left1">510169</td>
                            <td style='width:130px'>0.04</td>
                            <td class="cab_table_right1">2.07</td>
                         </tr>
                                                                                    <tr class="cab_table_tr1">
                            <td class="cab_table_left1">1193683</td>
                            <td style='width:130px'>0.23</td>
                            <td class="cab_table_right1">0.00</td>
                         </tr>
                                                                                    <tr class="cab_table_tr2">
                            <td class="cab_table_left1">2159860</td>
                            <td style='width:130px'>1.06</td>
                            <td class="cab_table_right1">51.54</td>
                         </tr>
                                                                                    <tr class="cab_table_tr1">
                            <td class="cab_table_left1">8070080</td>
                            <td style='width:130px'>1.76</td>
                            <td class="cab_table_right1">147.80</td>
                         </tr>
                                                                                    <tr class="cab_table_tr2">
                            <td class="cab_table_left1">8079297</td>
                            <td style='width:130px'>19.25</td>
                            <td class="cab_table_right1">884.85</td>
                         </tr>
                                                                            </table>
            </div>
        </div>
4

2 回答 2

0

这是我的解决方案,如果可以的话,修改它

foreach($lines as $num => $line) { 


                            $new1 = $xpath->query('//td[1]', $line);
                            $new2 = $xpath->query('//td[2]', $line);
                            $new3 = $xpath->query('//td[3]', $line);

                            foreach ($new1 as $value) {

                                $result['number'][$value->nodeValue] = $value->nodeValue;

                            }

                            foreach ($new2 as $value) {

                                $result['volume'][$value->nodeValue] = $value->nodeValue;

                            }

                            foreach ($new3 as $value) {

                                $result['code'][$value->nodeValue] = $value->nodeValue;

                            }


                }
于 2013-07-18T18:41:54.487 回答
0

如果有一个 XPath 表达式并在 3 元组中循环呢?

$cells = $xpath->query('//td');

for($i=0; $i<$cells->length; ) {
  $result['number'][] = $cells->item($i++)->nodeValue;
  $result['volume'][] = $cells->item($i++)->nodeValue;
  $result['code'][] = $cells->item($i++)->nodeValue;
}
于 2013-07-19T16:06:15.923 回答