我有一個HTML表格想解析爲JSON。PHP:解析HTML表格到JSON
Mata Uang | Jual | Beli |
---|---|---|
USD | 11.450,00 | 11.300,00 |
AUD | 11.094,00 | 10.494,00 |
CAD | 11.169,00 | 10.669,00 |
CHF | 12.719,00 | 12.219,00 |
EUR | 15.678,00 | 15.028,00 |
GBP | 18.525,00 | 17.725,00 |
HKD | 1.643,00 | 1.293,00 |
JPY | 118,87 | 113,37 |
SAR | 3.233,00 | 2.833,00 |
SGD | 9.454,00 | 8.854,00 |
而且我有一些PHP代碼,我從谷歌上搜索發現:
include("simple_html_dom.php");
$html = file_get_html('index.html');
$row_count=0;
$json = array();
foreach ($html->find('tr') as $row) {
$currency = $row->find('td',0)->innertext;
$sell = $row->find('td',1)->innertext;
$buy = $row->find('td',2)->innertext;
$json[$currency][$sell][$buy]=true;
}
echo json_encode($json);
?>
而我得到的是對的代碼,它似乎是錯誤的:
{
"":{
"":{
"":true
}
},
"USD":{
"11.450,00":{
"11.300,00":true
}
},
"AUD":{
"11.094,00":{
"10.494,00":true
}
},
"CAD":{
"11.169,00":{
"10.669,00":true
}
},
"CHF":{
"12.719,00":{
"12.219,00":true
}
},
"EUR":{
"15.678,00":{
"15.028,00":true
}
},
"GBP":{
"18.525,00":{
"17.725,00":true
}
},
"HKD":{
"1.643,00":{
"1.293,00":true
}
},
"JPY":{
"118,87":{
"113,37":true
}
},
"SAR":{
"3.233,00":{
"2.833,00":true
}
},
"SGD":{
"9.454,00":{
"8.854,00":true
}
}
}null
但我需要什麼是:
[{
"currency":"USD"{
"sell":"11.450,00"
"buy":"11.300,00"
},
"currency":"AUD"{
"sell":"11.094,00"
"buy":"10.494,00"
},
"currency":"CAD"{
"sell":"11.169,00"
"buy":"10.669,00"
},
"currency":"CHF"{
"sell":"12.719,00"
"buy":"12.219,00"
},
"currency":"EUR"{
"sell":"15.678,00"
"buy":"15.028,00"
},
"currency":"GBP"{
"sell":"18.525,00"
"buy":"17.725,00"
},
"currency":"HKD"{
"sell":"1.643,00"
"buy":"1.293,00"
},
"currency":"JPY"{
"sell":"118,87"
"buy":"113,37"
},
"currency":"SAR"{
"sell":"3.233,00"
"buy":"2.833,00"
},
"currency""SGD"{
"sell":"9.454,00"
"boy":"8.854,00"
}
}]
我認爲問題在於$ json變量,但很難完成。