php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #48721 Added parameter to pg_fetch_all allowing index to be column based
Submitted: 2009-06-29 15:31 UTC Modified: 2012-04-17 09:03 UTC
From: eric dot schultz at cyvon dot com Assigned: yohgaki (profile)
Status: Wont fix Package: PostgreSQL related
PHP Version: 5.2.10 OS: Linux
Private report: No CVE-ID: None
 [2009-06-29 15:31 UTC] eric dot schultz at cyvon dot com
Description:
------------
I would love if the pg_fetch_all could take a parameter that would (optionally) allow the indexing of the resultset to be a column value as opposed to 0-based. (as many tables include a primary key/id)

Reproduce code:
---------------
---
From manual page: function.pg-fetch-all
---
I find myself doing the following VERY often:

$res = pg_query("Select * from set_options");
if(!$res) return(0);
$ret = array();
while($r = pg_fetch_assoc($res)){
    $ret[$r['id']] = $r; }
return($ret);


Expected result:
----------------
above produces an associative array indexed on the "id" column. Would like this built into the pg_fetch_all

Actual result:
--------------
see above

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-01 23:13 UTC] jani@php.net
-Package: Feature/Change Request +Package: PostgreSQL related
 [2012-03-31 06:01 UTC] yohgaki@php.net
I think I marked the same request as "wont't fix". 
This sounds reasonable.
 [2012-03-31 06:01 UTC] yohgaki@php.net
-Assigned To: +Assigned To: yohgaki
 [2012-04-17 09:03 UTC] yohgaki@php.net
I forgot about compound keys. Therefore, it's simply cannot be done.

You can convert it with one line.

<?php
$res = array(array('id'=>1, 'a'=>'abc'), array('id'=>2, 'a'=>'xyz'));
array_walk($res, function($e, $k) use (&$new) {$new[$e['id']] = $e;});
var_dump($new);
 [2012-04-17 09:03 UTC] yohgaki@php.net
-Status: Assigned +Status: Wont fix
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC