如果我理解正确,解决方案就不必那么复杂。一个简单的 SELECT 查询来获取年份和值,然后您可以在 PHP 中使用循环并计算百分比。像这样的东西:
<?php
// Get all the data from the database.
$sql = "SELECT year, value FROM exports";
$stmt = $pdo->query($sql);
// An array to store the precentages.
$percentages = [];
// A variable to keep the value for the last year, to be
// used to calculate the percentage for the current year.
$lastValue = null;
foreach ($stmt as $row) {
// If there is no last value, the current year is the first one.
if ($lastValue == null) {
// The first year would always be 100%
$percentages[$row["year"]] = 1.0;
}
else {
// Store the percentage for the current year, based on the last year.
$percentages[$row["year"]] = (float)$row["value"] / $lastValue;
}
// Overwrite the last year value with the current year value
// to prepare for the next year.
$lastValue = (float)$row["value"];
}
结果数组如下所示:
array (
[1992] = 1.0,
[1993] = 1.2,
[1994] = 0.95
... etc ...
)