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

问题描述 投票:0回答:1

我使用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

这里最后一行的第4个单元格(列)是062018.现在如何查找包含相同列值的所有上述行(最后一行的第4列...即062018)然后得到第7个,第8个,要回显的那些行的第9列值(单元格值)。

我能够仅在最后一行获取值,但无法使用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循环并获取这些值...对于包含最后一行第4列的相同单元格值的所有行。

php loops for-loop foreach
1个回答
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);

?>
© www.soinside.com 2019 - 2024. All rights reserved.