使用JSON值执行操作

时间:2014-03-01 14:57:31

标签: php json

我需要总结" initialContractualPrice"的所有值。在http://www.base.gov.pt/base2/rest/contratos?&sort(-publicationDate)

我想在php中进行操作。

谁知道可以帮助我?

非常感谢你;)

2 个答案:

答案 0 :(得分:1)

尝试

$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_rvar_dump查看结构

答案 1 :(得分:0)

这应该照顾它。

// 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;