So my previous question: PHP Convert html table to JSON was quickly dismissed as a duplicate and I'm still struggling to get to what I need. I think it's mostly a logic problem in the loops and I need someone else to take a look at it.
Given this table as an example:
<table id="Details" class="DATA_TABLE DATA_TABLE_WO_TOTAL">
<tr>
<th>Application</th>
<th>Version number</th>
<th>Virtual Administration Server</th>
<th>Group</th>
<th>Device</th>
<th>Installed</th>
<th>Last visible time</th>
<th>Last connection to Administration Server</th>
<th>IP address</th>
</tr>
<tr>
<td class="sD">some text</td>
<td class="sD">10.2.5.3201</td>
<td class="sD"></td>
<td class="sD">Thin PC</td>
<td class="sD">PC#</td>
<td class="sD">date</td>
<td class="sD">date</td>
<td class="sD">date</td>
<td class="sD">ip address</td>
</tr>
<tr>
<tr>
<td class="sD">some more text</td>
<td class="sD">10.2.5.3201</td>
<td class="sD"></td>
<td class="sD">Thin PC</td>
<td class="sD">PC#</td>
<td class="sD">date</td>
<td class="sD">date</td>
<td class="sD">date</td>
<td class="sD">ip address</td>
</tr>
</table>
I need to create an array (that I can later convert to a json) where the th tags are the keys and then all the td tags inside each other tr is the data corresponding to these keys. I have the following php code:
<?php
$dom = new DOMDocument;
$dom->loadHTML($cleantable2); //this is the table above
$xpath = new DOMXPath($dom);
foreach($xpath->query('//table/tr') as $tr){
$tmp = [];
foreach($xpath->query('//table/tr/th', $tr) as $th){
$key = $th->textContent;
foreach($xpath->query('td', $tr) as $td){
$tmp[$key] = trim($td->textContent);
}
}
$result[]=$tmp;
}
var_dump($result);
?>
It does get the keys right, but not the data, sample output:
[89]=>
array(9) {
["Application"]=>
string(13) "192.168.6.104"
["Version number"]=>
string(13) "192.168.6.104"
["Virtual Administration Server"]=>
string(13) "192.168.6.104"
["Group"]=>
string(13) "192.168.6.104"
["Device"]=>
string(13) "192.168.6.104"
["Installed"]=>
string(13) "192.168.6.104"
["Last visible time"]=>
string(13) "192.168.6.104"
["Last connection to Administration Server"]=>
string(13) "192.168.6.104"
["IP address"]=>
string(13) "192.168.6.104"
}
As you can see, it only picks up the IP address for each key and not the rest of the data. What am I doing wrong? Can someone help out and not just dismiss this as a duplicate? Been trying to figure this out for over a day, I'm pretty sure my issue is just not looping correctly but I'm not seeing it...
Thanks
</div>