首页  >  问答  >  正文

如何在 PHP 中插入关联数组?

我需要在 PHP 中对 SQL 结果进行以下安排:

if (!$result) {
  echo "An error occurred.\n";
  exit;
}

while($array = pg_fetch_array($result)) {
  $list = $array['list'];
}

pg_free_result($result);

但只有 pedro 在数组中返回。

P粉762730205P粉762730205172 天前362

全部回复(1)我来回复

  • P粉330232096

    P粉3302320962024-04-03 15:44:42

    您需要 pg_fetch_assoc() 来获取返回的关联数组。

    并且您需要建立一个数组来附加节点:

    $lists = [];
    while ($row = pg_fetch_assoc($result)) {
      $lists[] = $row['list'];
    }

    或者如果你只是想玩一下,可以使用 pg_fetch_all_columns()。

    https://www.php.net /manual/en/function.pg-fetch-all-columns.php

    $lists = pg_fetch_all_columns($result, 0);

    0 表示行中的第一列。

    回复
    0
  • 取消回复