Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #2939
| From | "Peter H. Coffin" <hellsop@ninehells.com> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: Roll SQL name value query into two-dimensional array |
| Date | 2011-08-21 21:50 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <slrnj53gvk.icc.hellsop@nibelheim.ninehells.com> (permalink) |
| References | <9bd3264a-328b-4e98-8910-1fddd49c6bf4@c14g2000yqk.googlegroups.com> |
On Sun, 21 Aug 2011 18:35:52 -0700 (PDT), Richard A. DeVenezia wrote:
> I have a query that returns name value pairs in groups.
>
> data
> g n v
> 1 a 1
> 1 b 2
> 1 c 3
> 3 a 10
> 3 b 9
> 3 c 8
>
> want
> x[0] = array('a'=>1,'b'=>2,'c'=>3);
> x[1] = array('a'=>10,'b'=>9,'c'=>8);
>
> this is close but x is ending up empty
>
> $q = 'select g,n,v from some_view order by g,some_seq_id';
> $result = db_query($q); // in drupal
>
> $x=array();
> $map=array();
> $prior_g = -1;
> while ($obj = db_fetch_object($result)) {
> if ($rec->g != $prior_g) {
> //append a new map
> $map=$array();
So you create an empty array...
> $x[] = $map;
And tuck that into your array of objects...
> $prior_g = $rec->g;
> }
and throw away $obj ?
> // fill in the map as we roll through the name value pairs within
> the group
> $map[$rec->n] = $rec->v;
> }
>
> print "<pre>".print_r($x,1)."</pre>";
>
> shows
> Array
> (
> [0] => Array
> (
> )
>
> [1] => Array
> (
> )
> )
>
>
> So what is going wrong ?
--
For their next act, they'll no doubt be buying a firewall running under
NT, which makes about as much sense as building a prison out of
meringue.
-- Tanuki
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Roll SQL name value query into two-dimensional array "Richard A. DeVenezia" <rdevenezia@gmail.com> - 2011-08-21 18:35 -0700 Re: Roll SQL name value query into two-dimensional array Jerry Stuckle <jstucklex@attglobal.net> - 2011-08-21 22:15 -0400 Re: Roll SQL name value query into two-dimensional array "Peter H. Coffin" <hellsop@ninehells.com> - 2011-08-21 21:50 -0500 Re: Roll SQL name value query into two-dimensional array Jonathan Stein <jstein@image.dk> - 2011-08-22 14:21 +0200
csiph-web