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

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.

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

?>