php - 如何将具有不同数量结果的 Postgres 结果转换为 PHP 中的有序列表

标签 php arrays postgresql

我有两个表,一个是国家列表;另一个带有给定变量的年份和值列表。

第二个表仅包含特定年份的条目,这些条目因国家/地区而异。因此,当我加入表格(以获取国家名称,未显示在以下列表中)时,结果确实如下所示:

      id_country    year_start  value_ori
        886            2002      161.348
        886            2003      161.348
        886            2004      176.016
        886            2005      176.016
        886            2006      179.683
        886            2007      183.35
        886            2008      201.685
        886            2009      227.354
        886            2010      234.688
        886            2011      245.689
        886            2012      293.36
        886            2013      440.04
        620            1990      40.337
        620            1991      1056.096
        620            1992      1151.438
        620            1993      1389.793
        620            1994      1584.144
        620            1995      1631.815
        620            1996      1749.159
        620            1997      1796.83
        620            1998      1906.84
        620            1999      1664.818
        620            2000      1642.816
        620            2001      2016.85
        620            2002      1760.16
        620            2003      1873.837
        620            2004      1961.845
        620            2005      2310.21
        620            2006      2328.545
        620            2007      2361.548
        620            2008      3329.636
        620            2009      3069.279
        620            2010      3098.615
        620            2011      2823.59
        620            2012      3373.64
        620            2013      2948.268

也就是说,对于 886 这个国家,我没有早于 2002 年的值。

现在,我想用这个列表创建一个 CSV,它应该在下一年之后连续列出每个国家/地区的值 - 就像一个表格。这意味着如果某些年份没有值,则应该有一个空格(或“null”),如下所示:

          1990  1991   1992     1993      1994      1995     1996    1997     1998    1999    2000     2001     2002    2003    2004     2005    2006     2007     2008     2009     2010     2011     2012    2013
     620 40.337 1056.096 1151.438 1389.793 1584.144 1631.815 1749.159 1796.83 1906.84 1664.818 1642.816 2016.85 1760.16 1873.837 1961.845 2310.21 2328.545 2361.548 3329.636 3069.279 3098.615 2823.59 3373.64 2948.268
     886                                                                                                        161.348 161.348 176.016 176.016 179.683  183.35   201.685  227.354  234.688  245.689  293.36   440.04

现在,我没有成功编写一个循环来处理那些无效的“值”。至少不是一帆风顺的。我可以检索所有可用的年份,然后对每个年份和国家/地区启动查询。或者用结果构建一个三维数组,并将其用于循环。但这看起来像是奇怪的编程。

对于如何使用几行简洁的 PHP 代码来实现这一点,我将不胜感激。

最佳答案

循环遍历找到的最小年份和最大值,同时循环(内部)找到所有找到的公司,这样您就可以创建 csv 行,而不会遗漏任何年份或公司:

$company_ids = array();
$company_data = array();
$year_min = $year_max = 2016;

$year_fld = 'year_start';
$comp_fld = 'id_country';
$data_fld = 'value_ori';

foreach ($data as &$d) {
  $company_ids[$d[$comp_fld]] = $d[$comp_fld];
  $d[$year_fld] = (integer) $d[$year_fld];
  if ($year_min > $d[$year_fld]) $year_min = $d[$year_fld];
  if ($year_max < $d[$year_fld]) $year_max = $d[$year_fld];
  $company_data[$d[$year_fld]][$d[$comp_fld]] = $d[$data_fld];
}

//die("$year_min $year_max");

//ksort($company_data);
//die('<pre>'.print_r($company_data,true));

$csv_rows = array();
for ($year=$year_min; $year<$year_max+1; $year++) {
  // print rows or accumulate them, whatever
  foreach ($company_ids as &$cid) {
    $csv_rows[$year][$cid] = (empty($company_data[$year][$cid])) ? '' : $company_data[$year][$cid];
  }
}

die('<pre>'.print_r($csv_rows,true));

关于php - 如何将具有不同数量结果的 Postgres 结果转换为 PHP 中的有序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40286256/

相关文章:

sql - 将数组传递给 PostgreSQL PL/pgSQL 函数

php - 使用 PHP 从 MySQL 检索查询结果

php - WampServer->Mysql取不到数据(一直返回NULL)

c - 如何旋转矩阵? C

python - psycopg2 : does cursor. mogrify 防止 sql 注入(inject)?

java - MappingException : Could not get constructor for org. hibernate.persister.entity.SingleTableEntityPersister

php - Elasticsearch 模糊匹配,精确匹配最先显示

php - 如何在 Laravel 中的关系上应用reverse()?

带有多维数组的php菜单生成器

php - 如何让 PHP natcasesort 永久重新排列数组位置?