Re: Build Associative Array from database records
| From: | Miles Thompson | Date: | Wed, 11 Oct 2000 01:31:38 +0000 |
| Subject: | Re: Build Associative Array from database records | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-3503@lists.php.net to get a copy of this message | ||
Troy / Chris,
Thank-you both - your suggestions were nearly identical and worked. Now I see
the clue near the end of the discussion on array_push. Once again, many
thanks and I'm rolling.
Regards - Miles Thompson
Troy Settle wrote:
> > -----Original Message-----
> > From: Miles Thompson [mailto:milesthompson@sprint.ca]
> > Sent: Tuesday, October 10, 2000 3:08 PM
> > To: php-db@lists.php.net
> > Subject: [PHP-DB] Build Associative Array from database records
> >
> >
> > I've been picking away at this all day and getting nowhere. I want to
> > build an associative array from the results of a database query. This is
> > the array I want to end up with, call it arClasses, with this for
> > contents:
> > "1"=>"Lake Lot", "2"=>"Ocean Lot",
> > "3"=>"Forest Lot", "4"=>"Lake
> > Island", "5"=>"Ocean Island",
> > "6"=>"Luggage-luxury"
> > so that I can ask for arClasses["4"] and have "Lake Island" returned,
> > not "Ocean Lot", or "3".
> >
> > Here's what I have so far, database connection has been made ....
> > $arClasses = array();
> > $sql = "select * from class";
> > $result = mysql_query( $sql );
> > while( $row = mysql_fetch_array( $result ) )
> > {
> > $nClassKey = $row["nClassKey"];
> > $cClass = $row["cClass"];
> > array_push( $arClasses, $nClassKey, $cClass);
> > }
> >
>
> Try this:
>
> while ($row = mysql_fetch_array($result) {
> $arClasses[$row[cClassKey]] = $row[cClass];
> }
>
> HTH,
>
> --
> Troy Settle
> Pulaski Networks
> 540.994.4254
>
> It's always a long day, 86400 doesn't fit into a short