Php 在pg_fetch_数组之外定义数组键值
基本上,我想在while循环外定义Php 在pg_fetch_数组之外定义数组键值,php,arrays,postgresql,Php,Arrays,Postgresql,基本上,我想在while循环外定义$georesults数组及其键值对,但我希望数组由while循环内pg_fetch_array()中的行填充 while ($row = pg_fetch_array($result, NULL, PGSQL_ASSOC)) { $georesults['results'][] = array( "$columns[0]" => $row["$columns[0]"],
$georesults
数组及其键值对,但我希望数组由while循环内pg_fetch_array()
中的行填充
while ($row = pg_fetch_array($result, NULL, PGSQL_ASSOC)) {
$georesults['results'][] = array(
"$columns[0]" => $row["$columns[0]"],
"$columns[1]" => $row["$columns[1]"],
"$columns[2]" => $row["$columns[2]"],
"$columns[3]" => $row["$columns[3]"]
);
}
我不太清楚如何做到这一点
是否可以定义数组:
$georesults['results'][] = array(
"$columns[0]" => $row["$columns[0]"],
"$columns[1]" => $row["$columns[1]"],
"$columns[2]" => $row["$columns[2]"],
"$columns[3]" => $row["$columns[3]"]
);
在while循环之外,但是否根据pg\u fetch\u数组的结果填充它
之所以要这样做,是因为我想根据查询中定义的列动态设置键值对的数量。查询将经常更改。取决于用户选择的内容
问候
Geo//在循环外部声明数组变量
$results=array();
while ($row = pg_fetch_array($result, NULL, PGSQL_ASSOC)) {
$results[] = $row;
}
//you can use the result outside the loop.
or
$results=array();
while ($row = pg_fetch_array($result, NULL, PGSQL_ASSOC)) {
$results[] = array('id'=>$row['id'],'name'=>$row['name'], 'password'=>$row['password'] );
}
//use the result outside the loop
希望能有所帮助我意识到我需要做的就是使用pg_fetch_all()而不是pg_fetch_array(),后者返回查询中指定的列。