RE: [PHP-DB] Build Associative Array from database records

From: Date: Tue, 10 Oct 2000 20:21:35 +0000
Subject: RE: [PHP-DB] Build Associative Array from database records
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-3495@lists.php.net to get a copy of this message
> -----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

« previous php.db (#3495) next »