Reputation: 83
I found this topic: How to parse this table and extract data from it? Which probably is the answer for my problem, I want to the same thing - parse some external table, extract data and print this data on my webpage. @Yoshi gives perfect answer, but when I trying to use this code, I am getting empty array
Array ( )
I don't know why this doesn't work? I trying to use the same example
<?php
$dom = new DomDocument;
$dom->loadHtmlFile('http://www.nbs.rs/kursnaListaModul/srednjiKurs.faces?lang=lat');
$xpath = new DomXPath($dom);
// collect header names
$headerNames = array();
foreach ($xpath->query('//table[@id="index:srednjiKursLista"]//th') as $node) {
$headerNames[] = $node->nodeValue;
}
// collect data
$data = array();
foreach ($xpath->query('//tbody[@id="index:srednjiKursLista:tbody_element"]/tr') as $node) {
$rowData = array();
foreach ($xpath->query('td', $node) as $cell) {
$rowData[] = $cell->nodeValue;
}
$data[] = array_combine($headerNames, $rowData);
}
print_r($data);
?>
Upvotes: 2
Views: 961
Reputation: 384
Page has changed, that is why code is not working, this one works:
include('simple_html_dom.php');
$dom = new DomDocument;
$dom->loadHtmlFile('http://www.nbs.rs/kursnaListaModul/srednjiKurs.faces?lang=lat');
$xpath = new DomXPath($dom);
// collect header names
$headerNames = array();
foreach ($xpath->query('//table[@id="index:srednjiKursLista"]//th') as $node) {
$headerNames[] = $node->nodeValue;
}
// collect data
$data = array();
foreach ($xpath->query('//tbody[@id="index:srednjiKursList:tbody_element"]/tr') as $node) {
$rowData = array();
foreach ($xpath->query('td', $node) as $cell) {
$rowData[] = $cell->nodeValue;
}
$data[] = array_combine($headerNames, $rowData);
}
echo "<pre>";
print_r($data);
echo "</pre>";
Now, I suppose that you want to extract SREDNJI KURS value for some of the currencies, and you are going to do that
echo $data[15]['SREDNJI KURS'];
This is going to extract for USD.
Upvotes: 1