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);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!