我需要在http://www.base.gov.pt/base2/rest/contratos?&sort(-publicationDate)中總結「initialContractualPrice」的所有值。使用JSON值做操作
我需要做的操作在PHP中。
誰知道可以幫助我?
非常感謝你;)
我需要在http://www.base.gov.pt/base2/rest/contratos?&sort(-publicationDate)中總結「initialContractualPrice」的所有值。使用JSON值做操作
我需要做的操作在PHP中。
誰知道可以幫助我?
非常感謝你;)
嘗試
$data = file_get_contents('http://www.base.gov.pt/base2/rest/contratos?&sort(-publicationDate)');
$data = json_decode($data);
foreach($data as $dat){
echo $dat->publicationDate;
}
您還可以使用print_r
或var_dump
看結構
這應該照顧它。
// Get json from url
$url = 'http://www.base.gov.pt/base2/rest/contratos?&sort(-publicationDate)';
$content = file_get_contents($url);
// Decode json into an array
$json = json_decode($content, true);
// Set default
$total = 0;
// Loop through the array created when decoding json
foreach ($json as $key)
{
// remove symbol from the end leaving only digits
$value = substr(utf8_decode($key['initialContractualPrice']), 0, -1);
// remove the decimal point
$value = str_replace('.', '', $value);
// replace the comma with a decimal point
$value = str_replace(',', '.', $value);
// add this number to the total value
$total += $value;
}
echo $total;
非常感謝:D – user3332475
我保存網頁中的所有數據「http://www.base.gov.pt/base2/rest/contratos?&sort(-publicationDate)」,並且我需要將它恢復並執行操作...你知道我爲什麼能做到嗎? – user3332475
RTM:http://php.net/json_decode –
您是否確定允許在正式同意的情況下訪問這些JSON數據?它在'gov'域。如果沒有準備好處理後果。 –
@sankar是否有任何問題?我應該刪除我的答案嗎? –