I am scraping a website using Simple HTML DOM, the output looks like this:
<tr>
<th>Satuan</th>
<th>Harga Barang 1</th>
<th>Harga Barang 2</th>
<th>Harga Barang 3</th>
<th>Harga Barang 4</th>
</tr>
<tr>
<td>0.5</td>
<td>Rp 388.000</td>
<td>Rp 342.000</td>
<td>Rp 456.000</td>
<td>Rp 377.000</td>
</tr>
<tr>
<td>1.0</td>
<td>Rp 725.000</td>
<td>Rp 676.000</td>
<td>Rp 855.000</td>
<td>Rp 684.000</td>
</tr>
and this is my code:
<?php
include('simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file("mylink.com/blabla");
foreach($html->find('tr') as $e) {
echo $e;
}
?>
How to convert the output into arrays?
Here is the snippet,
$ret = $html->find('tr');
$i = true;
$headers = [];
foreach ($ret as $key => $value) {
if ($i) {
// fetching headers of first row
foreach ($value->find('th') as $cell) {
$headers[] = $cell->plaintext;
}
} else {
$temp = [];
// fetching pending values of td
foreach ($value->find('td') as $cell) {
$temp[] = $cell->plaintext;
}
// combining headers with values fetched from not first row
$result[] = array_combine($headers, $temp);
}
$i = false;
}
print_r($result);die;
Output
Array
(
[0] => Array
(
[Satuan] => 0.5
[Harga Barang 1] => Rp 388.000
[Harga Barang 2] => Rp 342.000
[Harga Barang 3] => Rp 456.000
[Harga Barang 4] => Rp 377.000
)
[1] => Array
(
[Satuan] => 1.0
[Harga Barang 1] => Rp 725.000
[Harga Barang 2] => Rp 676.000
[Harga Barang 3] => Rp 855.000
[Harga Barang 4] => Rp 684.000
)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.