duannian3494 2019-02-27 03:20
浏览 59
已采纳

PHP循环以在所有行中查找特定单元格值,其中行包含特定列

I have scraped a html table using simplehtmldom... the struture of the html table is ...here

0241657 02022202143 2018000003 042018 13552 1001 Basic Pay 32340.00 0 0241657 02022202143 2018000003 042018 13552 1006 Dearness Allowances 7795.00 0 0241657 02022202143 2018000003 042018 13552 1007 House Rent Allowance 6468.00 0 0241657 02022202143 2018000003 042018 13552 2003 APGLI Subscription 0 1150.00 0241657 02022202143 2018000003 042018 13552 2005 GIS Ins Fund 0 60.00 0241657 02022202143 2018000003 042018 13552 2006 Professional Tax 0 200.00 0241657 02022202143 2018000003 042018 13552 2043 CPS(New GPF) 0 4014.00 0241657 02022202143 2018000003 042018 13552 2091 EHF SUBSCRIPTION 0 90.00 0241657 02022202143 2018000004 052018 142720 1001 Basic Pay 32340.00 0 0241657 02022202143 2018000004 052018 142720 1006 Dearness Allowances 7795.00 0 0241657 02022202143 2018000004 052018 142720 1007 House Rent Allowance 6468.00 0 0241657 02022202143 2018000004 052018 142720 2003 APGLI Subscription 0 1150.00 0241657 02022202143 2018000004 052018 142720 2005 GIS Ins Fund 0 60.00 0241657 02022202143 2018000004 052018 142720 2006 Professional Tax 0 200.00 0241657 02022202143 2018000004 052018 142720 2043 CPS(New GPF) 0 4014.00 0241657 02022202143 2018000004 052018 142720 2091 EHF SUBSCRIPTION 0 90.00 0241657 02022202143 2018000009 062018 344121 1001 Basic Pay 33220.00 0 0241657 02022202143 2018000009 062018 344121 1006 Dearness Allowances 8007.00 0 0241657 02022202143 2018000009 062018 344121 1007 House Rent Allowance 6644.00 0 0241657 02022202143 2018000009 062018 344121 1008 City Compensatory Allowance 0.00 0241657 02022202143 2018000009 062018 344121 1025 Interim Relief 0.00 0 0241657 02022202143 2018000009 062018 344121 2003 APGLI Subscription 0 1150.00 0241657 02022202143 2018000009 062018 344121 2005 GIS Ins Fund 0 60.00 0241657 02022202143 2018000009 062018 344121 2006 Professional Tax 0 200.00 0241657 02022202143 2018000009 062018 344121 2043 CPS(New GPF) 0 4123.00 0241657 02022202143 2018000009 062018 344121 2091 EHF SUBSCRIPTION 0 90.00

Here the 4th cell(column) in the last row is 062018. Now How to find all the above rows which contains the same column value (4th Column of the last row... i.e.,062018) and then get the 7th,8th,9th Column values (Cell Values) of those rows to echo.

I am able to fetch values in only last row but unable to fetch the remaining above row cell values using simplehtmldom... like this

$mmyy = $html->find('table',1)->find('tr',-1)->find('td',3)->plaintext;
$tds = $html->find('table',1)->find('td');
foreach($tds as $td){

 if($td->plaintext == $mmyy){

    $td7 = $td->next_sibling()->next_sibling()->next_sibling();
    $td8 = $td->next_sibling()->next_sibling()->next_sibling()->next_sibling();
    $basic = $td7->plaintext ;   
    $pay = $td8->plaintext ; 
    break; 
 } }

 echo $basic;
 echo $pay;

how to php loop and get these values... for all the rows which contains the same cell value of the 4th column of the last row.

  • 写回答

1条回答 默认 最新

  • dongtui8593 2019-02-27 05:35
    关注
    <?php
    
    $input = "
    <table>
    <tr> <td>0241657</td> <td>02022202143</td><td>2018000003</td> <td>042018</td><td>13552</td> <td>2091</td>
    <td>EHF SUBSCRIPTION</td><td>0</td><td>90</td></tr>
    <tr><td>0241657</td> <td>02022202143</td><td>2018000009</td><td>062018</td> <td>344121</td><td>1006</td>
    <td>Dearness Allowances</td><td>8007.00 0</td></tr>
    </table>
    ";
    
    function getTD($text) {
        $text = explode("<td>",$text);
        return trim($text[1]);
    }
    
    $result = array();
    $input = explode("<tr>",$input); // get lines table
    array_shift($input); //delete up first line
    
    foreach($input as $str)
    {
        $str = explode("</td>",$str); // get td separate
        $dt = getTD($str[3]);
        if ($dt == "062018") // you if
        {
        $tores = array();
        for($i=5; $i<8; $i++) $tores[] = getTD($str[$i]); //copy values
        $result[] = $tores;
        }
    }
    
    print_r($result);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题