Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在pg_fetch_数组之外定义数组键值_Php_Arrays_Postgresql - Fatal编程技术网

Php 在pg_fetch_数组之外定义数组键值

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]"],

基本上,我想在while循环外定义
$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(),后者返回查询中指定的列。