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

时间:2019-02-27 03:20:41

标签: php loops for-loop foreach

我已经使用simplehtmldom抓取了html表... html表的结构是...这里

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

这是最后一行的第四个单元格(列)是062018。 现在,如何查找以上所有包含相同列值的行(最后一行的第4列...即062018),然后获取这些行的第7、8、9列值(单元格值)以进行回显。 / p>

我仅能获取最后一行中的值,但无法使用simplehtmldom来获取上面行单元格中其余的值...

$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;

如何php循环并获取这些值...对于包含最后一行第四列的​​相同单元格值的所有行。

1 个答案:

答案 0 :(得分:0)

<?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);

?>